Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uvicorn/FastAPI Duplicate Logging

My FastAPI app seems to log many things twice.

INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [21360] using statreload
INFO:     Started server process [21362]
INFO:uvicorn.error:Started server process [21362]
INFO:     Waiting for application startup.
INFO:uvicorn.error:Waiting for application startup.
INFO:     Application startup complete.
INFO:uvicorn.error:Application startup complete.
^CINFO:     Shutting down
INFO:uvicorn.error:Shutting down
INFO:     Waiting for application shutdown.
INFO:uvicorn.error:Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:uvicorn.error:Application shutdown complete.
INFO:     Finished server process [21362]
INFO:uvicorn.error:Finished server process [21362]
INFO:     Stopping reloader process [21360]

This includes any exceptions that are raised, you get the entire stack trace twice. I've seen some answers suggest just removing the log handlers for Uvicorn, but this feels wrong. What if there is a logging event that happens at the Uvicorn layer of the stack but not in FastAPI?

Is there a way to get only log output once without just overwriting uvicorn's log handlers?

like image 682
Ryan Sandridge Avatar asked Dec 07 '25 17:12

Ryan Sandridge


1 Answers

Just remove the uvicorn handlers:

logging.getLogger("uvicorn").handlers.clear()
like image 185
BaiJiFeiLong Avatar answered Dec 09 '25 08:12

BaiJiFeiLong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!