中间件,通过向项目日志中注入一个唯一的ID来支持单个请求响应循环跟踪
django-guid的Python项目详细描述
Django GUID
Django GUID将唯一的相关ID/请求ID附加到每个请求的所有日志输出。 换句话说,连接到请求的所有日志现在都有一个唯一的ID附加到它,使得调试变得简单。在
您应该使用哪个版本的Django GUID取决于您的Django版本以及您是运行ASGI还是WSGI服务器。 要确定应该使用哪个Django GUID版本,请参阅下表。在
Django version | Django-GUID version |
---|---|
3.1.1 or above | 3.x.x - ASGI and WSGI |
3.0.0 - 3.1.0 | 2.x.x - Only WSGI |
2.2.x | 2.x.x - Only WSGI |
Django GUID>;=3.0.0使用ContextVar来存储和访问GUID。存储到以前版本的对象, 使用当前线程的ID来访问它。(在Django2.2 LTS通过之前,Django GUID的版本2是受支持的。)
Resources:
示例
带有GUID的日志输出:
INFO ... [773fa6885e03493498077a273d1b7f2d] project.views This is a DRF view log, and should have a GUID. WARNING ... [773fa6885e03493498077a273d1b7f2d] project.services.file Some warning in a function INFO ... [0d1c3919e46e4cd2b2f4ac9a187a8ea1] project.views This is a DRF view log, and should have a GUID. INFO ... [99d44111e9174c5a9494275aa7f28858] project.views This is a DRF view log, and should have a GUID. WARNING ... [0d1c3919e46e4cd2b2f4ac9a187a8ea1] project.services.file Some warning in a function WARNING ... [99d44111e9174c5a9494275aa7f28858] project.services.file Some warning in a function
没有GUID的日志输出:
^{pr2}$更多示例请参见documentation。在
安装
使用pip安装:
pip install django-guid
设置
包设置已添加到您的settings.py:
DJANGO_GUID={'GUID_HEADER_NAME':'Correlation-ID','VALIDATE_GUID':True,'RETURN_HEADER':True,'EXPOSE_HEADER':True,'INTEGRATIONS':[],'IGNORE_URLS':[],'UUID_LENGTH':32,}
可选参数
GUID_HEADER_NAME
The name of the GUID to look for in a header in an incoming request. Remember that it’s case insensitive.
默认值:相关ID
VALIDATE_GUID
Whether the
GUID_HEADER_NAME
should be validated or not. If the GUID sent to through the header is not a valid GUID (uuid.uuid4
).默认值:True
RETURN_HEADER
Whether to return the GUID (Correlation-ID) as a header in the response or not. It will have the same name as the
GUID_HEADER_NAME
setting.默认值:True
EXPOSE_HEADER
Whether to return
Access-Control-Expose-Headers
for the GUID header ifRETURN_HEADER
isTrue
, has no effect ifRETURN_HEADER
isFalse
. This is allows the JavaScript Fetch API to access the header when CORS is enabled.默认值:True
INTEGRATIONS
Whether to enable any custom or available integrations with
django_guid
. As an example, usingSentryIntegration()
as an integration would set Sentry’stransaction_id
to match the GUID used by the middleware.默认值:[]
IGNORE_URLS
URL endpoints where the middleware will be disabled. You can put your health check endpoints here.
默认值:[]
UUID_LENGTH
Lets you optionally trim the length of the package generated UUIDs.
默认值:32
配置
设置好设置后,将以下内容添加到项目“settings.py”:
1。已安装的应用程序
将django_guid
添加到您的INSTALLED_APPS
:
INSTALLED_APPS=[...'django_guid',]
2。中间件
将django_guid.middleware.guid_middleware
添加到您的MIDDLEWARE:
MIDDLEWARE=['django_guid.middleware.guid_middleware',...]
建议您在顶部添加中间件,以便剩余的中间件记录器包含请求GUID。在
3。日志记录配置
在LOGGING配置中添加django_guid.log_filters.CorrelationId
作为筛选器:
LOGGING={...'filters':{'correlation_id':{'()':'django_guid.log_filters.CorrelationId'}}}
将过滤器放入处理程序:
LOGGING={...'handlers':{'console':{'class':'logging.StreamHandler','formatter':'medium','filters':['correlation_id'],}}}
并确保将新的correlation_id筛选器添加到一个或所有格式化程序中:
LOGGING={...'formatters':{'medium':{'format':'%(levelname)s%(asctime)s [%(correlation_id)s] %(name)s%(message)s'}}}
如果这些设置令人困惑,请查看演示项目' settings.py文件以获取完整的示例。在
4。Django GUID记录器(可选)
如果希望看到Django GUID中间件输出,可以为模块配置一个记录器。 只需将django_guid添加到项目中的记录器中,如下例所示:
LOGGING={...'loggers':{'django_guid':{'handlers':['console','logstash'],'level':'WARNING','propagate':False,}}}
这在实现包时特别有用,如果您计划将现有的guid传递给中间件,因为配置错误的guid不会引发异常,而是会生成警告日志。在
- 项目
标签: