[QUESTION] How to do logging in a FastApi container, any logging does not appear
See original GitHub issueDescription I have another project that utilizes fast api using gunicorn running uvicorn workers and supervisor to keep the api up. Recently I came across the issue that none of my logs from files that are not the fast api app are coming through. Initially I tried making an adhoc script to see if it works as well as changing the levels of the logging. I only had success if I set the logging to be at the DEBUG level.
I put together another small project to test out if I would run into this problem with a clean slate and I still couldn’t get logging working with a standard
import logging
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
log.info('help!')
Other steps I took was chmod-ing the /var/log/ directory in case it was a permissions issue but I had no luck. Has anyone else ran into this or have recommendations on how they implemented logging?
Additional context For context I put up the testing repo here: https://github.com/PunkDork21/fastapi-git-test Testing it would be like:
docker-compose up -d
docker exec -it git-test_web_1 bash
python3 ./appy.py
The most of the files are similar to what I have in my real project
Issue Analytics
- State:
- Created 4 years ago
- Reactions:50
- Comments:51 (1 by maintainers)

Top Related StackOverflow Question
I’ve struggled with this for the past few days as well, and only just figured it out. The HTTP request info is stored in the
uvicorn.accesslogs. In order to see that information when runninguvicornviagunicorn(lots of unicorns here!), you’ll need the following snippet in yourmain.pyThis will allow the
gunicorn.errorlogger to handle theuvicorn.accesslogger, thus allowing the HTTP request information to come through. You don’t even need to add--access-log -in thegunicorncommand (but thank you for the suggestion, @bcb!) Big, huge thanks to @slhck and @bcb for pointing me in this direction. I hope that this helps others!I finally solved it!
First, make sure you set the environment variable
LOG_LEVELto debug, e.g. in your Docker-Compose file.Now in your actual FastAPI app, add this code below the imports:
This way, if your app is loaded via gunicorn, you can tell the logger to use gunicorn’s log level instead of the default one. Because if gunicorn loads your app, FastAPI does not know about the environment variable directly; you will have to manually override the log level.
The
elsebranch is for when you run the app directly, in which case I assume debug logging will be required.I tested this with the version where the command
/start-reload.shis specified in the Docker-Compose config, as well as the one where it is left out, and of course running the app directly.