Skip to content

Looking for support: Setup logging for async function with http.aio #398

Description

@HFabi

Hello.
My goal is to see the logs of an async CloudRun function in the CloudRun logs console.
Does anybody has experience how to set this up correctly?

Background

So far, for synchronous CloudRun function, I always used the google python-logging library, which is also recommended in the Google Cloud documentation and is really simple.

However, the same is not working when I switch to the async function, there I never receive any logs. Here the sample code:

from starlette.requests import Request
from starlette.responses import Response, JSONResponse
import aiohttp
import functions_framework.aio

import google.cloud.logging

# logging handler
client = google.cloud.logging.Client()
client.setup_logging(log_level=logging.DEBUG)


@functions_framework.aio.http
async def llm_emulator(request: Request):
    logging.log(msg="hello world", level=logging.DEBUG) #-> this log never reaches CloudRun log console

I did some experimentation, my assumptions so far:

  • async uses a different setup, not Flask+uvicorn but starlette under the hood. This might require some different initialisation of the logging
  • maybe data is logged into a different thread/coroutine, I tried client.flash_handlers() but this has no effect.

Does anyone use logs in an async function and has experience, how to set it up correctly? (with or without any library)
I would be grateful for any hints or links to available documentation. Thanks a lot.

Activity

  1. changed the title [-]Looking for advice: Setup logging for async function with http.aio[/-] [+]Looking for support: Setup logging for async function with http.aio[/+] on Sep 22, 2025
  2. taeold commented on Sep 22, 2025

    @taeold
    Contributor

    Hey @HFabi - thanks for trying out async function support and a detailed write up of the issue.

    I'm going to dig a bit deeper, but I'm wondering if the auto-detection of async function is accidentally overriding your log configuration.

    If you have sometime, can you try adding the following env var to force manual detection of async function in your Run function and seeing if the issue persists?

    FUNCTION_USE_ASGI=true
    
  3. HFabi commented on Sep 23, 2025

    @HFabi
    Author

    Hey @taeold,

    thanks for your fast response. When I set the env variable FUNCTION_USE_ASGI=true then the logging is working as expected!
    With the python-logging setup the same way as in synchronous functions.

    So, would it be safe for production code to use the async feature with this flag as env variable?

  4. taeold commented on Sep 23, 2025

    @taeold
    Contributor

    @HFabi Thanks for giving it a try. Glad that it worked.

    Yes, env var is a supported feature, and it happens to fix this issue because using the env var bypasses the code that was overriding your logging config. I'll send a patch to fix that issue later in the week, but in the meantime feel free to use it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions