Django:如何记录来自管理命令的异常?

2024-04-29 07:50:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我不会收到命令错误的邮件。在

python deebate\manage.py test_logging_errors --settings=deebate.settings.local --traceback

命令:

^{pr2}$

很明显这会

UnicodeEncodeError: 'ascii' codec can't encode character u'\xbb' in position 2: ordinal not in range(128)

我有DEBUG = False

日志设置为

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler',
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple',
        },
       # I always add this handler to facilitate separating loggings
        'debug_log_file':{
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(ROOT_PROJECT_INTERNAL, 'logs', 'debug.log'),
            'maxBytes': '16777216', # 16megabytes
            'backupCount': 10,
            'formatter': 'verbose'
        },
        'warning_log_file':{
            'level': 'WARNING',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(ROOT_PROJECT_INTERNAL, 'logs', 'warning.log'),
            'maxBytes': '16777216', # 16megabytes
            'backupCount': 10,
            'formatter': 'verbose'
        },
        'django_log_file':{
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(ROOT_PROJECT_INTERNAL, 'logs', 'django.log'),
            'maxBytes': '16777216', # 16megabytes
            'backupCount': 10,
            'formatter': 'verbose'
        },

    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins', 'django_log_file'],
            'level': 'ERROR',
            'propagate': True,
        },
        'core': {
            'handlers': ['mail_admins', 'debug_log_file', 'warning_log_file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    }
}

我还有哨兵。在

debug.log和{}由应用程序定期填充。在

我怎样才能抓住这个例外?为什么Django不明白?在


Tags: djangodebuglogverboseformatterlogginghandlersmail
2条回答

我很欣赏this article,它解释了如何设置AdminEmailHandler,还解释了如何修改{}以捕获异常并记录它们,以便所有管理命令都将发送电子邮件。在

您可以将AdminEmailHandler设置为从管理命令中获取错误电子邮件

相关问题 更多 >