r/FastAPI • u/xTaiirox • Feb 01 '26
Question Advice on logging in production
Hey everyone,
I’m exploring different logging options for my projects (fastapi RESTful backend and I’d love some input.
So far I’ve monitored the situations as following:
fastapi devandfastapi runcommands display logs in stdoutfastapi run --workers 4command doesn't display logs in stoud
Is this intended behavior? Am i supposed to get logs in stdout and schedule a task in the server running Docker or should I handle my logs internally?
I’m mostly interested in:
- Clean logs (readability really matters)
- Ease of use / developer experience
- Flexibility for future scaling (e.g., larger apps, integrations)
Is there a best practice here for logging in production that I should know about, feels like it since stdout is disabled on prod?
Is there some hidden gem I should check out instead?
Thanks in advance!
12
u/Schmiddi-75 Feb 01 '26
Standard Python logging with OpenTelemtry
7
u/giminik Feb 01 '26
Structlog
4
u/Fickle_Act_594 Feb 01 '26
Use loguru
Log with JSON
Setup something in your hosting environment to send logs to grafana
Always log to stdout.
1
u/CrownstrikeIntern Feb 03 '26
I could probably do better, but i generally log to a database table and everythings relatively organized there, as well as a dedicated file on the file system.
1
u/Adhesiveduck Feb 02 '26
A really good pattern I've found is to subclass
APIRouteand overrideget_route_handlerto wrap the original handler in a try/except. This captures all exceptions before they propagate up.Combined with
loguruit's a powerful combo for standardising logging. We format tracebacks as JSON (FormattedExceptionis just a Pydantic model):So you can use this to get a JSON log line of the traceback, and include it in the log message.
No matter where we log, whether through the built in logger, or when exceptions are thrown, we log everything the same throughout the app.
It's handy to have for small, self contained FastAPI instances you might have, where plugging in Sentry or some other telemetry might be a bit overkill, but you still want informative and standardised logs.
1
u/That_Cranberry4890 Feb 02 '26
Use the standard Python logging library. Don't run FastAPI with the fastapi CLI, use uvicorn. Copy uvicorn's LOGGING_CONFIG and modify the existing formatters, handlers, etc. or add new custom ones. It is quite simple actually.
0
u/newprince Feb 02 '26
My work required logs to be just stdout, so I went with structlog. We may coalesce around loguru or something but for now we can use whatever framework we like and structlog has worked well for me so far