Django设置默认日志记录 [英] Django Setup Default Logging
问题描述
我似乎无法弄清楚如何为我的Django安装设置一个默认记录器。我想在 settings.py
中使用Django 1.3的新的 LOGGING
设置。
我查看了 Django Logging Doc的例子,但它看起来像我们只是设置处理程序,将为特定的记录器进行日志记录。在他们的例子的情况下,他们为名为'django','django.request'和'myproject.custom'的记录器设置处理程序。
我想做的所有设置默认值为$ code> logging.handlers.RotatingFileHandler ,默认情况下将处理所有日志记录。即,如果我在项目中的某个地方创建一个新的模块,并且它被表示为: my_app_name.my_new_module
,我应该可以做到这一点,并将所有日志记录转到旋转文件日志
#在文件'./my_app_name/my_new_module.py'
导入日志
logger = logging.getLogger('my_app_name.my_new_module')
logger.debug('Hello logs!')#< - 这应该记录到我在settings.py中设置的RotatingFileHandler!
b
$ b
通过使用空字符串引用catch all记录器:''
。
例如,在以下设置中,将所有日志事件保存到 logs / mylog.log
中,除了 django.request
将被保存到 logs / django_request.log
的日志事件。因为'传播'
设置为 False
为我的 django.request
logger,日志事件永远都不会到达'catch all'记录器。
LOGGING = {
'版本':1,
'disable_existing_loggers':True,
'formatters':{
'standard':{
'format':'%(asctime)s [%级别名)s]%(名称)s:%(消息)s'
},
},
'处理程序':{
'default':{
'level':'DEBUG',
'class':'logging.handlers.RotatingFileHandler',
'filename':'logs / mylog.log',
'maxBytes':1024 * 1024 * 5,#5 MB
'backupCount':5,
'formatter':'standard',
},
'request_handler':{
' ':'DEBUG',
'class':'logging.handlers.RotatingFileHandler',
'filename':'logs / django_request.log',
'maxBytes':1024 * 1024 * 5,#5 MB
'backupCount':5,
'formatter' '标准',
},
},
'loggers':{
'':{
'handlers':['default'],
'level':'DEBUG',
'propagate':True
},
'django.request':{
'handlers':['request_handler'],
'level':'DEBUG',
'propagate':False
},
}
}
I can't seem to figure out how to setup a "default" logger for my Django installation. I would like to use Django 1.3's new LOGGING
setting in settings.py
.
I've looked at the Django Logging Doc's example, but it looks to me like they only setup handlers which will do logging for particular loggers. In the case of their example they setup handler for the loggers named 'django','django.request', and 'myproject.custom'.
All I want to do is setup a default logging.handlers.RotatingFileHandler
which will handle all loggers by default. i.e., if I make a new module somewhere in my project and it is denoted by something like: my_app_name.my_new_module
, I should be able to do this and have all logging goto the rotating file logs.
# In file './my_app_name/my_new_module.py'
import logging
logger = logging.getLogger('my_app_name.my_new_module')
logger.debug('Hello logs!') # <-- This should get logged to my RotatingFileHandler that I setup in `settings.py`!
Figured it out...
You set the 'catch all' logger by referencing it with the empty string: ''
.
As an example, in the following setup I have the all log events getting saved to logs/mylog.log
, with the exception of django.request
log events which will be saved to logs/django_request.log
. Because 'propagate'
is set to False
for my django.request
logger, the log event will never reach the the 'catch all' logger.
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
},
},
'handlers': {
'default': {
'level':'DEBUG',
'class':'logging.handlers.RotatingFileHandler',
'filename': 'logs/mylog.log',
'maxBytes': 1024*1024*5, # 5 MB
'backupCount': 5,
'formatter':'standard',
},
'request_handler': {
'level':'DEBUG',
'class':'logging.handlers.RotatingFileHandler',
'filename': 'logs/django_request.log',
'maxBytes': 1024*1024*5, # 5 MB
'backupCount': 5,
'formatter':'standard',
},
},
'loggers': {
'': {
'handlers': ['default'],
'level': 'DEBUG',
'propagate': True
},
'django.request': {
'handlers': ['request_handler'],
'level': 'DEBUG',
'propagate': False
},
}
}
这篇关于Django设置默认日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!