问题
I've got a module I want to log in Django that looks something like this:
import logging
logger = logging.getLogger(__name__)
def foo():
#this is a test of logging
logger.info("info foo")
logger.warning("warn foo")
logger.error("error foo")
My LOGGING in settings.py is set to the following:
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'handlers': {
'null': {
'level':'DEBUG',
'class':'django.utils.log.NullHandler',
},
'console':{
'level':'DEBUG',
'class':'logging.StreamHandler',
'formatter': 'simple'
},
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
}
},
'loggers': {
'django': {
'handlers':['null', 'console'],
'propagate': True,
'level':'INFO',
},
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': False,
},
}
}
The output when I run a view calling foo is as follows:
WARNING:root:warn foo
ERROR:root:error foo
I would've expected the info log as well, but ok, let's try again but this time with every 'level' parameter in LOGGING set to 'INFO':
WARNING:root:warn foo
ERROR:root:error foo
OK... well how about setting everything to 'ERROR'?:
WARNING:root:warn foo
ERROR:root:error foo
LOGGING commented out?
WARNING:root:warn foo
ERROR:root:error foo
I've set DEBUG = False and even bothered to manually set
LOGGING_CONFIG = 'django.utils.log.dictConfig'
Does anyone have any other ideas? I don't understand why Django is ignoring my LOGGING setting.
回答1:
As usual, typing it up on stack overflow lead me to realize my mistake.
Rather than calling
logger = logging.getLogger(__name__)
I needed to call the desired logger specifically:
logger = logging.getLogger('django')
This has solved the problem.
来源:https://stackoverflow.com/questions/11971231/django-1-4-logging-variable-in-settings-py-seems-to-be-ignored