'Fast API Python special log parameter for each request
Maybe someone here have a good idea on how to solve my issue. I have a REST API project driven by FastAPI. Every incomming request comes with a hash in the header. I am looking for a simple solution to write this hash as an extra parameter to the logs. I want to avoid adding it every time per hand. I first come up with the solution to write a Middleware which writes the hash in a Logger Object and then later use the loggerObject.log() function which adds the hash automatically. But this only works for my own log messages. Log messages from for example exceptions or from libraries I use dont have the extra parameter.
Solution 1:[1]
I solved a similar problem using the structlog
library. It allows you to set global context variables that will be outputed to every log line.
Note that this only applies to logs outputted by structlog, special configuration is required in order to output the stdlib logging logs with these context variables.
Here is a demo FastAPI
application I wrote that generates a request-id for every incoming http request, and adds the request ID to every log line outputted during that HTTP request.
In this example I have configured the stdlib logging logs to be processed by structlog, so them to have the request-id parameter added to them.
https://gitlab.com/sagbot/structlog-demo
Reference files in demo:
- demo/route.py - A bunch of demo routes. note the
/long
route outputs both stdlib logs and structlog logs, but both of them are outputted with the request-id property - demo/configure-logging.py - This func configures both structlog and stdlib logging. Note that the "Special configuration" required to show the context variables in stdlib logs is in this func, where I added the custom formatters in
logging.config.dictConfig(...)
func - demo/middlewares/request_logging/middleware.py - This is the middleware that generates and adds the request-id to the logs. You can alter this middleware to not generate the value, but extract it from the hash header of the incomming request, as you wanted
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sagiv Oulu |