FastAPI logging
Here is how I setup my python logging for a fastAPI project.
I have the following fastAPI file architecture:
main.py
logging.conf
uicheckapp/
--db.py
--services.py
...
First I create a logging.conf file at the root of the project.
logging.conf
This file configures the loggers. I created the root and uicheckapp loggers. The root logger is a special logger. It is the logger that will be used if no other is found.
The uicheckapp logger has the same name as the package in which I have all my code I want to log from. This is very important, because a logger is selected by module name.
Main.py
At the beginning of the file I set the loggers by loading the “logging.conf” file. This needs to be done once before any “logging.getLogger(…)” calls. This setup can be done in an external dependency that is imported.