6

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

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

然后我也有哨兵。

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

我如何捕捉到那个异常?为什么 Django 不明白这一点?

4

2 回答 2

13

我很欣赏这篇文章,它解释了如何设置你的AdminEmailHandler,但也修改你manage.py以捕获异常并记录它们,以便所有管理命令都将发送电子邮件。

于 2016-03-30T12:02:06.870 回答
2

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

于 2013-05-27T16:24:33.827 回答