使用Django 1.5.1:
DEBUG = False
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(message)s'
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'verbose',
},
},
'loggers': {
# root logger
'': {
'handlers': ['console'],
},
#'django.request': {
# 'handlers': ['console'],
# 'level': 'DEBUG',
# 'propagate': False,
#},
}
}
如果我取消注释注释注释行并调用一个有1/0
行的视图,那么回溯将打印到控制台:
ERROR 2013-11-29 13:33:23,102 base Internal Server Error: /comment/*******/
Traceback (most recent call last):
...
File "*****/comments/views.py", line 10, in post
1/0
ZeroDivisionError: integer division or modulo by zero
WARNING 2013-11-29 13:33:23,103 csrf Forbidden (CSRF cookie not set.): /comment/******/
[29/Nov/2013 13:33:23] "POST /comment/******/ HTTP/1.0" 500 27
但是,如果这些行保持注释状态,则不会向控制台打印回溯,只是:
[29/Nov/2013 13:33:23] "POST /comment/******/ HTTP/1.0" 500 27
我认为如果没有配置django.request
记录器,它将传播到根记录器,后者会将所有内容打印到控制台.
我没有找到任何关于django.request
是特别的信息.
为什么它不起作用呢?
Here我读到:
在Django 1.5之前,日志(log)记录设置总是覆盖默认的Django日志(log)记录配置.从Django 1.5开始,可以将项目的日志(log)配置与Django的默认值合并,因此您可以决定是添加还是替换现有配置.
如果日志(log)记录指令Config中的DISABLE_EXISTING_LOGERS键设置为True(默认值),则默认配置将被完全覆盖.或者,您可以通过将DISABLE_EXISTING_LOGERS设置为FALSE来重新定义部分或全部记录器.
在django/utils/log.py
年内:
# Default logging for Django. This sends an email to the site admins on every
# HTTP 500 error. Depending on DEBUG, all other log records are either sent to
# the console (DEBUG=True) or discarded by mean of the NullHandler (DEBUG=False).
DEFAULT_LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse',
},
'require_debug_true': {
'()': 'django.utils.log.RequireDebugTrue',
},
},
'handlers': {
'console':{
'level': 'INFO',
'filters': ['require_debug_true'],
'class': 'logging.StreamHandler',
},
'null': {
'class': 'django.utils.log.NullHandler',
},
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django': {
'handlers': ['console'],
},
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': False,
},
'py.warnings': {
'handlers': ['console'],
},
}
}
因此,默认情况下,django.request
的值为propagate = False
.但在我的情况下,我有'disable_existing_loggers': True
个.