When running FastAPI app, all the logs in console are from Uvicorn and they do not have timestamp and other useful information. As Uvicorn applies python logging module, we can override Uvicorn logging formatter by applying a new logging configuration.
Meanwhile, it's able to unify the your endpoints logging with the Uvicorn logging by configuring all of them in the config file log_conf.yaml.
Before overriding:
uvicorn main:app --reloadINFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [34318] using StatReload
INFO: Started server process [34320]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 127.0.0.1:50062 - "GET / HTTP/1.1" 200 OK
After applying log_conf.yaml:
uvicorn main:app --reload --log-config=log_conf.yaml2023-03-08 15:40:41,170 - uvicorn.error - INFO - Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
2023-03-08 15:40:41,170 - uvicorn.error - INFO - Started reloader process [34322] using StatReload
2023-03-08 15:40:41,297 - asyncio - DEBUG - Using selector: EpollSelector
2023-03-08 15:40:41,432 - uvicorn.error - INFO - Started server process [34324]
2023-03-08 15:40:41,432 - uvicorn.error - INFO - Waiting for application startup.
2023-03-08 15:40:41,432 - uvicorn.error - INFO - Application startup complete.
2023-03-08 15:48:21,450 - main - INFO - request / endpoint!
2023-03-08 15:48:21,450 - uvicorn.access - INFO - 127.0.0.1:59782 - "GET / HTTP/1.1" 200logs with FastAPI and Uvicorn · tiangolo/fastapi · Discussion #7457 · GitHub
Python Comprehensive Logging using YAML Configuration · GitHub
What is the significance of "ext" in this stream definition?
stream: ext://sys.stdoutI have issues where my app is not logging to journalctl with the name of the app defined in the conf file but rather as the name of the script that starts it in the Exec. So, am wondering if either pythons buffering or this definition could cause this? Can't find what "ext" means in the logging docs.
Also, it seems like some discussion should be devoted to simply logging to the uvicorn tree:
logger = logging.getLogger(f"uvicorn.{__name__}")