I am working on FastAPI - Uvicorn. I want to disable the logging by uvicorn. I need only the logs which are logged by the server.
I referred to this blog and implemented the logging.
I am working on FastAPI - Uvicorn. I want to disable the logging by uvicorn. I need only the logs which are logged by the server.
I referred to this blog and implemented the logging.
You could change log level for getting only needed messages, there are a bunch of possible options:
uvicorn main:app --log-level critical
I think I had the same issue.
To disable the logger one must first find the loggers, that shall be disabled. I did this by following this stackoverflow post and this GitHub-Issue.
For me it works to disable just two loggers from uvicorn:
import logging
# ....CODE....
uvicorn_error = logging.getLogger("uvicorn.error")
uvicorn_error.disabled = True
uvicorn_access = logging.getLogger("uvicorn.access")
uvicorn_access.disabled = True
At first I tried the answer provided by @Sanchouz, but this didn't work out for me - Further setting propagate = false is by some regarded as a bad practice (see this) . As I wanted to do it programmatically I couldn't test the answer provided by @funnydman.
Hope this helps anyone, thinklex.
I had a similar problem and I found a solution. In my case, I created a small website with FastAPI to launch web scrappers in separate processes. I also created class-wrapper for loggers from logging module. My problem was: when I started my app with uvicorn all the output from any web scrapper would go into console. I have a lot of stuff to log, so it's quite a problem, besides the fact, that I just don't need that output to go there, I use files to store logs.
When you get your logger, just set it's propagate property to False like this:
logger = logging.getLogger(logger_name)
logger.propagate = False
After spending some time debugging insides of a logging module I found a function, that applies all handlers to a given log record (it's a class, that stores all information about, let's call it, logger.log() call). It's function called callHandlers inside module's __init__ file. It's description stated, that it'll loop through handlers of current logger and it's parents.
This exactly what's happened: at first my logger's handlers processed log record, then root logger's handlers did the same. And when it did so, a message appeared in console. I didn't applied any handlers for a root logger myself, so unicorn did it.
And to fix this all I had to do is disable propagation, so my application's log records don't get handled by handlers of a root logger, which I showed in previous section.