django - 禁用使用 Django 登录 gunicorn 以获取特定请求/URL/端点
问题描述
这个问题类似于Disable logging in gunicorn for a specific request/URL/endpoint除了我的问题是关于在 Django 应用程序中禁用 gunicorn 健康检查日志记录。
如何在 Django 应用程序中禁用 gunicorn 登录?
我也在使用系统日志,所以settings.LOGGING
字典是这样的:
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"console": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"formatter": "default",
},
"syslog": {
"level": "DEBUG",
"class": "logging.handlers.SysLogHandler",
"formatter": "default",
"facility": SysLogHandler.LOG_LOCAL2,
"address": syslog_address,
},
},
"loggers": {
"django": {"handlers": ["console", "syslog"], "propagate": True},
"apps": {"handlers": ["console", "syslog"], "level": "DEBUG"},
"utils": {"handlers": ["console", "syslog"], "level": "DEBUG"},
"gunicorn.access": {"handlers": ["console", "syslog"], "level": "INFO"},
"gunicorn.error": {"handlers": ["console", "syslog"], "level": "INFO"},
},
}
请注意,我明确添加了gunicorn.access
用于 syslog 的记录器(请参阅:https ://github.com/benoitc/gunicorn/issues/2016 )。
解决方案
我在回答我自己的问题。
一旦我添加了gunicorn.access
要处理的记录器syslog
,我就不能简单地pre_request
在我的文件中添加一个钩子gunicorn.conf
(根据这个解决方案)。
您必须按照文档gunicorn.glogging.Logger
中的说明通过子类化来提供您自己的自定义日志记录类。
创建一个日志模块并添加:
from gunicorn import glogging
class CustomGunicornLogger(glogging.Logger):
def access(self, resp, req, environ, request_time):
# disable healthcheck logging
if req.path in ["/healthcheck"]:
return
super().access(resp, req, environ, request_time)
在您的gunicorn.conf
文件中,添加:
logger_class = 'your_module.CustomGunicornLogger'
gunicorn.conf
使用gunicorn 时使用您的文件:
gunicorn --config=gunicorn.conf your_django_app.wsgi:application
推荐阅读
- xcode - 启动画面在设备和情节提要上看起来不同
- laravel - 如何检查数据库中的用户密码是否为空并将该信息传递给 Vue 应用程序?
- php - 替换“特定”类别的商店和档案页面上的“添加到购物车”文本
- python - Python,编写 XML 文件 - 'charmap' 编解码器无法编码字符。包含要修复的编码时,get 必须是 str,而不是 bytes
- javascript - 如何确定正常矩形和旋转矩形之间是否存在碰撞?
- javascript - (JavaScript) 从用户输入框中查找圆的面积
- amazon-web-services - 在安全性方面使用 ec2 复制亚马逊工作场所
- asp.net-core-webapi - 带有 EF 核心的 Identity Server 4
- reactjs - 使用 SearchWidget 点击结果后如何防止SuggestedResult 崩溃?
- python - 在循环中制作字典时,当键可以在每次循环迭代中更改时,如何将数据写入 CSV?