python 3.9
azure-functions 1.11.2
fastapi 0.82.0
Function app runtime: 4
I wrote a very simple function with a single FastAPI route to test how logging works. The code I wrote is as follows:
import azure.functions as func
from fastapi import FastAPI, APIRouter
import logging
FUNC_PREFIX = "/api"
app = FastAPI(
docs_url=f"{FUNC_PREFIX}/docs",
openapi_url=f"{FUNC_PREFIX}/openapi.json",
)
azrouter = APIRouter()
@azrouter.get(
"/",
description="Test log",
response_model=str,
)
def get_bonuses():
logging.info("Test log")
return "Test logging"
def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
return func.AsgiMiddleware(app).handle(req, context)
app.include_router(azrouter, prefix=FUNC_PREFIX)
The API works fine, but unfortunately the log doesn't work properly. Locally nothing is printed on the console and the same thing happens on the function monitor.
If I change the FastAPI route from synchronous to asynchronous as follow everything works fine. I can see both the log locally and on the function monitor
@azrouter.get(
"/",
description="Test log",
response_model=str,
)
async def get_bonuses():
logging.info("Test log")
return "Test logging"
Why does this behavior occur? Is it possible to have synchronous routes with working log? If so how?