Your own logging.info lines never show in docker logs for a FastAPI app
logger.info() calls inside a FastAPI endpoint never appear in docker logsShort answer
docker logs shows only what the process writes to stdout and stderr. Python's root logger drops INFO below WARNING, and a logger with no handler writes nowhere useful. Set the level and a handler in your app, and tell Gunicorn to log at the same level.
Fix it fast
1Find out which cause you have
Print the effective level of your logger inside the container. WARNING (30) means INFO and DEBUG lines are dropped before any handler sees them.
docker compose exec web python -c "import logging; print(logging.getLogger().getEffectiveLevel())"2Match what you see, then jump to its fix
What does the error look like?
logger.info() calls inside a FastAPI endpoint never appear in docker logslogging.info() works with python main.py but not in the containerNo API endpoint logs in docker logs under gunicorn
Cause 1 of 2
The root logger is still at its default level, WARNING
1Check
Python's logging module tracks only WARNING and above until the program configures it. A child logger without its own level uses its parent's, and the root logger starts at WARNING.
docker compose exec web python -c "import logging; log=logging.getLogger('app'); print(log.getEffectiveLevel(), log.handlers)"2Fix
Configure logging once at start-up, before the first logger call, and use a module-level logger.
# app/main.py
import logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(name)s %(levelname)s %(message)s",
)
logger = logging.getLogger(__name__)
@app.get("/ping")
async def ping():
logger.info("ping called")
return {"ok": True}3Verify
Call the endpoint and read the container log.
curl -s http://localhost:8000/ping
docker compose logs --tail 20 webConfirmed by docs.python.org
Cause 2 of 2
Gunicorn does not forward what your workers print to its logs
1Check
Gunicorn sends its own error log to stderr and has no access log unless you ask for one. Whether it also captures what your app prints is controlled by capture_output, which is off by default. Read the flags in your Dockerfile or compose command.
docker compose config | grep -n -i -E "gunicorn|log"2Fix
Run Gunicorn with --capture-output so stdout and stderr of workers reach the error log, set --log-level to match, and open the access log on stdout.
# add these flags to your existing gunicorn command
gunicorn app.main:app --log-level info --capture-output \
--access-logfile - --error-logfile -3Verify
Rebuild, start the container and check that both access lines and your own lines appear.
docker compose up -d --build
docker compose logs -f webConfirmed by gunicorn.org
How do I stop it coming back?
- Configure logging in one place at start-up and import the logger everywhere else.
- Write to stdout or stderr in containers and let Docker collect the stream.
- Keep the Gunicorn log level and the Python log level in step.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
Related fixes
Servers
docker logs prints nothing for a Flask or Python container
the output appears only after the app exits, or in large delayed bursts
docker logs only shows what the process writes to standard output and standard error.
2 causes, each with a fixServers
Django returns a 500 behind Gunicorn and the traceback is nowhere
The traceback shows locally with runserver but not under Gunicorn
With DEBUG set to False, Django's default logging does not print errors to the console.
2 causes, each with a fixServers
pip install confluent-kafka fails because librdkafka/rdkafka.h is missing
pip stops while building confluent-kafka from source
The error means pip is compiling confluent-kafka from source, and the compiler cannot find the librdkafka C headers.
3 causes, each with a fixServers
pip install cryptography tries to compile and fails in a Docker build
fatal error: 'openssl/aes.h' file not found
cryptography ships prebuilt wheels.
3 causes, each with a fixServers
Docker build fails with apk: not found in a Python or Ubuntu image
/bin/sh: 1: apk: not found
apk is the package manager of Alpine Linux only.
2 causes, each with a fixServers
Docker cannot find manage.py or Permission denied on '/manage.py'
PermissionError: [Errno 13] Permission denied: '/manage.py'
The container is looking for manage.py in a directory where the file is not.
3 causes, each with a fix