Django:如何从管理命令中记录异常?

时间:2013-05-27 16:20:09

标签: python django exception error-handling command

我没有收到命令中发生错误的邮件。

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

命令:

# -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand, CommandError

import logging
logger = logging.getLogger(__name__)

    class Command(BaseCommand):
        help = "test unicode and logging"

        def handle(self, *args, **options):
            print(u"|`»|Ð".encode('ascii'))

显然这会抛出

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

我有DEBUG = False

LOGGING设置为

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,
        },
    }
}

然后我也有Sentry。

debug.logwarning.log定期由应用填充。

我如何捕获该异常?为什么Django没有抓住它?

2 个答案:

答案 0 :(得分:7)

我感谢this article解释了如何设置AdminEmailHandler,还修改了manage.py以捕获异常并将其记录下来,以便所有管理命令都会发送电子邮件。< / p>

答案 1 :(得分:2)

您可以设置AdminEmailHandler以从管理命令

收到错误电子邮件