Django Setup Default Logging Django Setup Default Logging django django

Django Setup Default Logging


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        },    }}


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:

root - this will be the configuration for the root logger. Processing of the configuration will be as for any logger, except that the propagate setting will not be applicable.

Here's the configuration from your answer changed to use the root key:

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',        },    },    'root': {        'handlers': ['default'],        'level': 'DEBUG'    },    'loggers': {        'django.request': {            'handlers': ['request_handler'],            'level': 'DEBUG',            'propagate': False        },    }}

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.


import logginglogger = logging.getLogger(__name__)

after add:

logging.basicConfig(    level = logging.DEBUG,    format = '%(name)s %(levelname)s %(message)s',)

we may change format to:

format = '"%(levelname)s:%(name)s:%(message)s"  ',

or

format = '%(name)s %(asctime)s %(levelname)s %(message)s',