I'm trying to setup my Django app and am running into problems configuring logging in settings.py file.
Django documentation and Python's logging documentation states that using:
'disable_existing_loggers': False
will allow me to use the existing logging configuration so that I don't have to repeat myself. The default logging in this case is the DEFAULT_LOGGING dict found in django.utils.log.py
When I try to use existing filters in DEFAULT_LOGGING
, require_debug_true
, in my one of my handlers for LOGGING_CONFIG
in settings.py, I get a KeyError when trying to run runserver.
I also get the same error when trying to use existing handlers in my logger, such as console
. The only reason I can think of is that somehow Django is disregarding the disable_existing_loggers
flag.
Has anyone run into this issue before? Thanks for the help.
File "/usr/lib/python3.4/logging/config.py", line 750, in add_handlers
logger.addHandler(self.config['handlers'][h])
File "/usr/lib/python3.4/logging/config.py", line 317, in __getitem__
value = dict.__getitem__(self, key)
KeyError: 'console'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.4/logging/config.py", line 611, in configure
self.configure_logger(name, loggers[name])
File "/usr/lib/python3.4/logging/config.py", line 775, in configure_logger
self.common_logger_config(logger, config, incremental)
File "/usr/lib/python3.4/logging/config.py", line 767, in common_logger_config
self.add_handlers(logger, handlers)
File "/usr/lib/python3.4/logging/config.py", line 752, in add_handlers
raise ValueError('Unable to add handler %r: %s' % (h, e))
ValueError: Unable to add handler 'console': 'console'
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'activity': {
'format': '[%(levelname)s] %(pathname)s <%(funcName)s>[%(lineno)s] : %(message)s',
},
'debug': {
'format': '[%(levelname)s] %(pathname)s <%(funcName)s>[%(lineno)s] : %(message)s',
},
},
'handlers': {
'debug': {
'level': 'DEBUG',
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': '/var/www/app/log/debug.log',
'formatter': 'debug',
'backupCount': 48,
'when': 'H',
},
'activity': {
'level': 'INFO',
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': '/var/www/app/log/activity.log',
'formatter': 'activity',
'backupCount': 48,
'when': 'H',
},
'error': {
'level': 'ERROR',
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': '/var/www/app/log/error.log',
'formatter': 'activity',
'backupCount': 48,
'when': 'H',
},
'syslog': {
'level': 'INFO',
'class': 'logging.handlers.SysLogHandler',
'formatter': 'activity',
'facility': SysLogHandler.LOG_LOCAL2,
'address': '/dev/log',
},
},
'loggers': {
'app.activity': {
'handlers': ["activity", "error", "debug"],
'level': 'DEBUG',
'propagate': True,
},
'django.request': {
'handlers': ["mail_admins", "error", "activity", "debug"],
'level': 'ERROR',
'propagate': False,
},
'django.security': {
'handlers': ["mail_admins", "error", "activity"],
'level': 'ERROR',
'propagate': False,
},
'py.warnings': {
'handlers': ["console", "debug"],
},
},
}
The easiest way to keep the default logging configuration and customize only some settings is by updating django.utils.log.DEFAULT_LOGGING
that is used by Django to configure logging:
from django.utils.log import DEFAULT_LOGGING
# Enable logging to console from our modules by configuring the root logger
DEFAULT_LOGGING['loggers'][''] = {
'handlers': ['console'],
'level': 'INFO',
'propagate': True
}
This is has to be in settings.py
as logging will be configured immediately after settings have been imported.
In my opinion you need to add configuration of console
Handler and the rest of handlers, filters and formatters (which you use) from django.utils.log.py
to logging configuration.
disable_existing_loggers
parameter regards only to loggers (not to handler, filter and formatter)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With