我没有收到来自命令中发生的错误的邮件。
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.log
和warning.log
定期由应用程序填充。
我如何捕捉到那个异常?为什么 Django 不明白这一点?