简体   繁体   English

Django - 记录唯一ID

[英]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). 我正在为每个传入的请求分配一个唯一的ID,并希望每当完成日志记录时自动将其包含在日志中(不在所有地方明确地包含它)。 I am using a middleware to generate and store this unique ID in a thread local. 我正在使用中间件在本地线程中生成并存储此唯一ID。

I want to modify Django's logging formatter to include this unique ID directly: 我想修改Django的日志格式化程序,直接包含这个唯一的ID:

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? 如何在不显式添加每个日志语句的情况下将唯一的id组件添加到格式化程序中?

Use a Filter : the technique is described in this post . 使用Filter :本文介绍了技术。 Though it refers to IP addresses, you can just as easily use the approach to log your unique request IDs. 虽然它涉及IP地址,但您可以轻松地使用该方法记录您的唯一请求ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM