Unable to view logs on a function app using FastAPI synchronous routes

a.salogni 1 Reputation point
2023-01-05T15:48:58.427+00:00

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?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,299 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MuthuKumaranMurugaachari-MSFT 22,146 Reputation points
    2023-01-11T14:25:39.3766667+00:00

    @a.salogni Thank you for reaching out to Microsoft Q&A. I found issue #11 with fast-api samples related to logging on synchronous operations and from the related main issue #1158, our product team has identified the cause and the fix is being worked upon. I would recommend tracking the main issue for the update. Sorry for the inconvenience caused by this issue.

    I hope this answers your question and feel free to add a comment if you have any other questions. Please ‘Upvote’ if it helped so that it can help others in the community.

    1 person found this answer helpful.