Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - logging unique ID

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?

like image 821
jeffreyveon Avatar asked Nov 01 '11 07:11

jeffreyveon


1 Answers

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.

like image 100
Vinay Sajip Avatar answered Sep 18 '22 12:09

Vinay Sajip