【发布时间】:2015-02-26 23:51:20
【问题描述】:
我正在尝试设置我的 Django 应用,但在 settings.py 文件中配置登录时遇到问题。
Django 文档和 Python 的日志记录文档指出使用:
'disable_existing_loggers': False 将允许我使用现有的日志记录配置,这样我就不必重复自己了。在这种情况下,默认日志记录是在django.utils.log.py 中找到的 DEFAULT_LOGGING dict
当我尝试使用 DEFAULT_LOGGING、require_debug_true 中的现有过滤器时,我在 settings.py 中的 LOGGING_CONFIG 处理程序之一中尝试运行 runserver 时出现 KeyError。
尝试在我的记录器中使用现有处理程序时,我也会遇到同样的错误,例如console。我能想到的唯一原因是 Django 以某种方式无视 disable_existing_loggers 标志。
以前有人遇到过这个问题吗?感谢您的帮助。
File "/usr/lib/python3.4/logging/config.py", line 750, in add_handlers
logger.addHandler(self.config['handlers'][h])
File "/usr/lib/python3.4/logging/config.py", line 317, in __getitem__
value = dict.__getitem__(self, key)
KeyError: 'console'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.4/logging/config.py", line 611, in configure
self.configure_logger(name, loggers[name])
File "/usr/lib/python3.4/logging/config.py", line 775, in configure_logger
self.common_logger_config(logger, config, incremental)
File "/usr/lib/python3.4/logging/config.py", line 767, in common_logger_config
self.add_handlers(logger, handlers)
File "/usr/lib/python3.4/logging/config.py", line 752, in add_handlers
raise ValueError('Unable to add handler %r: %s' % (h, e))
ValueError: Unable to add handler 'console': 'console'
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'activity': {
'format': '[%(levelname)s] %(pathname)s <%(funcName)s>[%(lineno)s] : %(message)s',
},
'debug': {
'format': '[%(levelname)s] %(pathname)s <%(funcName)s>[%(lineno)s] : %(message)s',
},
},
'handlers': {
'debug': {
'level': 'DEBUG',
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': '/var/www/app/log/debug.log',
'formatter': 'debug',
'backupCount': 48,
'when': 'H',
},
'activity': {
'level': 'INFO',
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': '/var/www/app/log/activity.log',
'formatter': 'activity',
'backupCount': 48,
'when': 'H',
},
'error': {
'level': 'ERROR',
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': '/var/www/app/log/error.log',
'formatter': 'activity',
'backupCount': 48,
'when': 'H',
},
'syslog': {
'level': 'INFO',
'class': 'logging.handlers.SysLogHandler',
'formatter': 'activity',
'facility': SysLogHandler.LOG_LOCAL2,
'address': '/dev/log',
},
},
'loggers': {
'app.activity': {
'handlers': ["activity", "error", "debug"],
'level': 'DEBUG',
'propagate': True,
},
'django.request': {
'handlers': ["mail_admins", "error", "activity", "debug"],
'level': 'ERROR',
'propagate': False,
},
'django.security': {
'handlers': ["mail_admins", "error", "activity"],
'level': 'ERROR',
'propagate': False,
},
'py.warnings': {
'handlers': ["console", "debug"],
},
},
}
【问题讨论】:
-
能否在 settings.py 中显示您的日志记录配置?
-
我想这可能会有所帮助...已添加。