中间件,通过向项目日志中注入一个唯一的ID来支持单个请求响应循环跟踪

django-guid的Python项目详细描述


Django GUID

https://img.shields.io/pypi/v/django-guid.svghttps://img.shields.io/badge/python-3.6+-blue.svghttps://img.shields.io/badge/django-2.2%20|%203.0%20|%203.1%20-blue.svghttps://img.shields.io/badge/ASGI-supported-brightgreen.svghttps://img.shields.io/badge/WSGI-supported-brightgreen.svghttps://readthedocs.org/projects/django-guid/badge/?version=latesthttps://codecov.io/gh/snok/django-guid/branch/master/graph/badge.svghttps://img.shields.io/badge/code%20style-black-000000.svghttps://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=whiteDjango GUID将唯一的相关ID/请求ID附加到每个请求的所有日志输出。 换句话说,连接到请求的所有日志现在都有一个唯一的ID附加到它,使得调试变得简单。在

您应该使用哪个版本的Django GUID取决于您的Django版本以及您是运行ASGI还是WSGI服务器。 要确定应该使用哪个Django GUID版本,请参阅下表。在

Django versionDjango-GUID version
3.1.1 or above3.x.x - ASGI and WSGI
3.0.0 - 3.1.02.x.x - Only WSGI
2.2.x2.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 if RETURN_HEADER is True, has no effect if RETURN_HEADER is False. 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, using SentryIntegration() as an integration would set Sentry’s transaction_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不会引发异常,而是会生成警告日志。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java我可以使用Hibernate连接不同的数据库并从表中导入数据吗?没有预定义的对象类   java为什么getBoundsInLocal获取错误的坐标?   java在处理并发哈希映射时必须使用锁   java将过滤的JTable数据导出到文本文件   从命令行编译java不使用库   Java使用方法和构造函数绘制图形   java理解优先级队列中的排序方法   尝试解密文件时,java CipherInputStream为空   在Java中将字符串解析为长字符串   java我想在raspberry pi重新启动时在crontabe中执行arecord命令   弹性搜索弹性搜索Lucene公式的Java计算   java使用apachepoi和docx4j读取doc文件   swing显示JavaGUI是否需要特殊处理?   java使用字符串进行数学运算   java在SpringMVC中用JSP初始化菜单项选择表单数据的最佳方法   无多线程的java多客户端程序   swing如何在Java中对BuffereImage进行去饱和?   java在安卓中创建自定义地图