Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Your own logging.info lines never show in docker logs for a FastAPI app

Errorlogger.info() calls inside a FastAPI endpoint never appear in docker logs

Short 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.

  • Applies to Python 3.12, FastAPI, Gunicorn, Docker Compose v2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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())"
  2. 2Match what you see, then jump to its fix

    1. 1warning() and error() lines show up, info() lines never doThe root logger is still at its default level, WARNINGFix
    2. 2Messages appear with python main.py but not when the same app is started by gunicornGunicorn does not forward what your workers print to its logsFix

What does the error look like?

  • logger.info() calls inside a FastAPI endpoint never appear in docker logs
  • logging.info() works with python main.py but not in the container
  • No 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 web

Confirmed by docs.python.org

Did this fix it?

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 web

Confirmed by gunicorn.org

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.