使用 DEBUG=False,如何将 django 异常记录到日志文件中 [英] With DEBUG=False, how can I log django exceptions to a log file

查看:46
本文介绍了使用 DEBUG=False,如何将 django 异常记录到日志文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 DEBUG=True 时,Django 异常转储到 stderr,它通常由 Web 服务器发送到旋转日志文件.

With DEBUG=True, Django exceptions dump to stderr, which is typically sent to a rotating log file by the web server.

当 DEBUG=False 时,Django 将异常通过电子邮件发送到 ADMINS=.

With DEBUG=False, Django instead emails the exception to the the ADMINS=.

如何使用 DEBUG=False 保留 DEBUG=True 行为?

How can I retain the DEBUG=True behavior with DEBUG=False?

我已经阅读了 您如何记录服务器错误django 站点如何查看错误日志Django 视图您如何记录服务器错误Django 网站.答案似乎涉及一些中间件.是否有可用的代码片段,或者是否包含这些电池?

I've read How do you log server errors on django sites and How can I see error logs of Django views and How do you log server errors on django sites. The answer seems to involve some middleware. Is there a code snippet available, or are these batteries included?

推荐答案

这是一个完整的工作日志配置.将严重错误记录到 sentry,通过电子邮件向管理员发送警告,将正常通知错误记录到 syslog,并在标准输出中提示调试消息.

Here is a full working logging configuration. Critical errors are logged to sentry, warnings are sent to admins by emails, normal notice errors are logged to syslog, and debug messages are prompted on the standard output.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'formatters': {
        'verbose': {
            'format': '[contactor] %(levelname)s %(asctime)s %(message)s'
        },
    },
    'handlers': {
        # Send all messages to console
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        },
        # Send info messages to syslog
        'syslog':{
            'level':'INFO',
            'class': 'logging.handlers.SysLogHandler',
            'facility': SysLogHandler.LOG_LOCAL2,
            'address': '/dev/log',
            'formatter': 'verbose',
        },
        # Warning messages are sent to admin emails
        'mail_admins': {
            'level': 'WARNING',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler',
        },
        # critical errors are logged to sentry
        'sentry': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'raven.contrib.django.handlers.SentryHandler',
        },
    },
    'loggers': {
        # This is the "catch all" logger
        '': {
            'handlers': ['console', 'syslog', 'mail_admins', 'sentry'],
            'level': 'DEBUG',
            'propagate': False,
        },
    }
}

这篇关于使用 DEBUG=False,如何将 django 异常记录到日志文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆