Reputation: 1037
So I have two loggers in my Django project. One for authentication failures, and one which includes those but also includes messages for when something is edited (basically everything for which I have a logger command). I seem to have a bit of a problem in modules where I want to use both loggers however. My two loggers are currently defined like so:
'': {
'handlers': ['file'],
'level': 'INFO',
'propagate': True,
},
'auth': {
'handlers': ['file_auth'],
'level': 'CRITICAL',
'propagate': True,
}
And my handlers are:
'file': {
'level': 'INFO',
'class': 'logging.FileHandler',
'filename': '/home/debug.log',
'formatter': 'simple',
},
'file_auth': {
'level': 'CRITICAL',
'class': 'logging.FileHandler',
'filename': '/home/debug2.log',
'formatter': 'verbose',
},
At the top of my Django view, I have
import logging
logger = logging.getLogger('')
logger = logging.getLogger('auth')
Then within one view I have a logger.info(message). If I change this to logger.critical(message), the message appears in both log files but if it remains as logger.info, nothing happens at all.
(Probably useless information...at the start of my LOGGING section in settings.py, I have:
'version': 1,
'disable_existing_loggers': False,
Not sure if they have any relevance. But previously I was struggling to get the errors to appear in both files until I switched the order of introducing them which magically changed stuff - I really don't understand why that would make a difference though)
Would be really grateful if someone could help me out....Probably really simple but I must admit I don't really understand how it works..
Upvotes: 0
Views: 90
Reputation: 99317
You have two different loggers, named ''
(the root logger) and 'auth'
. The order in which your two statements appear:
logger = logging.getLogger('')
logger = logging.getLogger('auth')
obviously makes a difference when you call
logger.info(...)
as in the two cases you will be calling the method on two different loggers. You might wish to change your code to
root_logger = logging.getLogger('')
logger = logging.getLogger('auth')
and then call methods on either root_logger
or logger
as appropriate.
Upvotes: 1