I am assigning a unique ID to every incoming request and want to this to be automatically included in the logs whenever logging is done (without explicitly including it in all places). I am using a middleware to generate and store this unique ID in a thread local.
I want to modify Django's logging formatter to include this unique ID directly:
LOGGING = {
...
'formatters': {
'full': {
'format': '%(asctime)s %(levelname)s %(name)s - %(message)s',
}
}
...
}
How do I add the unique id component to the formatter without explicitly adding at each log statement?
Use a Filter
: the technique is described in this post. Though it refers to IP addresses, you can just as easily use the approach to log your unique request IDs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With