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`!
after add:
we may change format to:
or
I made a quick sample to check what configuration is used when both
root
key and the empty''
logger are referenced in config dict.Prints the following result:
indicating that configuration under
root
key has the highest priority. If the block is removed, the result is:In both case, I was able to debug and determine that all three loggers (
l1
,l2
androot
) referenced the same logger instance, the root logger.Hope that will help others who, like me, were confused by the 2 different ways to configure the root logger.
As you said in your answer, Chris, one option to define a default logger is to use the empty string as its key.
However, I think the intended way is to define a special logger under the
root
key of the logging configuration dictionary. I found this in the Python documentation:Here's the configuration from your answer changed to use the
root
key:To be fair, I can't see any difference in behaviour between the two configurations. It appears that defining a logger with an empty string key will modify the root logger, because
logging.getLogger('')
will return the root logger.The only reason I prefer
'root'
over''
is that it is explicit about modifying the root logger. In case you were curious,'root'
overrides''
if you define both, just because the root entry is processed last.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 ofdjango.request
log events which will be saved tologs/django_request.log
. Because'propagate'
is set toFalse
for mydjango.request
logger, the log event will never reach the the 'catch all' logger.