Azure-Samples / fastapi-on-azure-functions

A sample to run a FastAPI app on Azure Functions
MIT License
94 stars 70 forks source link

Logging not working for synchronous path operations #11

Open ManelBH opened 1 year ago

ManelBH commented 1 year ago

This issue is for a: (mark with an x)

- [x] bug report
- [ ] feature request
- [ ] documentation issue or request
- [ ] regression (a behavior that used to work and stopped in a new release)

Minimal steps to reproduce

@app.get("/sample")
def index():
    logging.info("sample")
    return {
        "info": "Try /hello/Shivani for parameterized route.",
    }

Expected/desired behavior

Expected: A line with the logged text appears in the logs (same behaviour as with async operations) Actual: Nothing is logged.

OS and Version?

Windows 10

Versions

Python version: 3.9.10 Core Tools Version: 4.0.4704 Function Runtime Version: 4.7.3.18953 azure-functions==1.12.0 fastapi==0.88.0

ManelBH commented 1 year ago

Related: https://github.com/Azure/azure-functions-python-worker/issues/1158

bhagyshricompany commented 1 year ago

Thanks for reporting we will discuss this feature issue and update you on the inputs soon.

Qmatteo commented 1 year ago

@bhagyshricompany Any update on this?

bhagyshricompany commented 1 year ago

@Qmatteo its in progress

Qmatteo commented 1 year ago

@bhagyshricompany any idea on the timeline?

bhagyshricompany commented 1 year ago

will update

Qmatteo commented 1 year ago

@bhagyshricompany "will update" ???

hashPhoeNiX commented 1 year ago

@bhagyshricompany Any update yet?

GiuseppeChiesa-TomTom commented 1 year ago

I'm facing the same issue, I see another linked issue has a merged PR but I'm still observing the problem with fastapi

ajstewart commented 1 year ago

Another "I'm facing the same issue" post, would it be possible for someone to give an update please so we don't have to guess?

As Giuseppe said above I can see that https://github.com/Azure/azure-functions-python-worker/pull/1171 has been merged 2 weeks ago but there has not been a core tools release yet. This issue is still open: https://github.com/Azure/azure-functions-python-worker/issues/1158 so it's hard to know what the status of this is.

It would be much appreciated! Without the logging on these synchronous defined functions suddenly the overall Azure Function I was building becomes impossible to monitor.

asalogni commented 1 year ago

@bhagyshricompany any update on this? The fix described in this thread https://github.com/Azure/azure-functions-python-worker/issues/1158 doesn't seem to work for me

Qmatteo commented 1 year ago

@bhagyshricompany ANY UPDATES ON THIS, PLEASE?

EvanR-Dev commented 1 year ago

Hi, I just wanted to add a solution to this. We can fetch the current logger with getLogger() and use it accordingly. Here is a sample:

# logger.py
import logging

loggers = {}

def myLogger(name):
    global loggers

    if loggers.get(name):
        return loggers.get(name)
    else:
        logger = logging.getLogger(name)
        logger.setLevel(logging.INFO)
        handler = logging.StreamHandler()
        logger.addHandler(handler)
        loggers[name] = logger

        return logger
# app.py
from logger import myLogger
# and your other imports

@api_router.get(
    "/hello/{name}",
    description="Get name"
)
def get_name(name: str):
    logger = myLogger(__name__)
    logger.info(f"/hello/ endpoint. Name: {name}.")

    return {
        "name": name,
    }

The log in get_name() shows up upon execution as intended. Note that there may be issues with tracking as handle() (non-async) is deprecated

nikie commented 6 months ago

I have posted a workaround in the https://github.com/Azure/azure-functions-python-worker/issues/1158 thread's comment

anguspmitchell commented 3 months ago

To anyone else here, @nikie 's solution worked for me, although not out-of-the-box (makes some assumptions about the server you're using)

Also found that certain types of logging didn't work for me on Azure. More here - @nikie your solution worked for me, although not out of the box, since I was using some custom middleware written a couple years ago.

I also experienced some other quirks with Azure logging. I expanded upon it here - https://stackoverflow.com/a/78738009/5469472