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

查看:995
本文介绍了使用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?

推荐答案

这是一个完整的日志记录配置。严重错误记录到哨兵,警告通过电子邮件发送给管理员,正常的通知错误记录到系统日志,调试消息在标准输出上提示。

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天全站免登陆