How to log errors and request body to a seperate log file while also sending a premade response to client? - FastAPI with uvicorn and gunicorn #6504
-
First Check
Commit to Help
Description and Example CodeWhat I am trying to achieve: Here's how I am running gunicorn and tried to capture it: Here's how i am handling validation errors and where i presume changes need to be made: Some errors like, when https requests are sent to http api, they get printed to stderr, that uvicorn prints and these do get captured in file specified in gunicorn's "--error-log-file" flag. but, this file will also include other gunicorn's details like starting the server etc,. So, while logging gunicorn's details to a log-file, how do I also track the error and the request body that led to the error into a separate log file with the timestamp? Operating SystemLinux Operating System Detailsdebian-bullseye-slim inside a docker image. FastAPI Versionfastapi==0.71.0 Python VersionPython 3.8.12 Additional Contextno additional context. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
You can use pythons logging module to setup a logger that logs to specific file (https://docs.python.org/3/howto/logging.html#logging-basic-tutorial) Once you have the above logger you can pass the config (file or dict) to gunicorn using https://docs.gunicorn.org/en/stable/settings.html#logconfig Once you have your custom logger you can log to it using a custom route/ APIRoute class https://fastapi.tiangolo.com/advanced/custom-request-and-route/?h=logging#custom-request-and-apiroute-class |
Beta Was this translation helpful? Give feedback.
You can use pythons logging module to setup a logger that logs to specific file (https://docs.python.org/3/howto/logging.html#logging-basic-tutorial)
Once you have the above logger you can pass the config (file or dict) to gunicorn using https://docs.gunicorn.org/en/stable/settings.html#logconfig
Once you have your custom logger you can log to it using a custom route/ APIRoute class https://fastapi.tiangolo.com/advanced/custom-request-and-route/?h=logging#custom-request-and-apiroute-class