From bfaf5d881f93446ccd14c9566a7c30b08eb26aa2 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Wed, 22 Jul 2015 13:45:16 +0200 Subject: [PATCH] add a custom logger class to make log level dynamic in LOGGING (fixes #7906) A new logger class named DjangoLogger is created in hobo.logger, a DebugLogLevel subclass of `int` is used to mark log level which should change based upong a boolean Django setting. In DjangoLogger.getEffectiveLevel() if the level found is a DebugLogLevel, the corresponding setting is checked and if it is True, the DEBUG level is returned. --- debian/debian_config_common.py | 9 ++++++--- hobo/logger.py | 22 ++++++++++++++++++++++ hobo/wsgi.py | 3 +++ manage.py | 3 +++ 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 hobo/logger.py diff --git a/debian/debian_config_common.py b/debian/debian_config_common.py index 6e1668c..aab7184 100644 --- a/debian/debian_config_common.py +++ b/debian/debian_config_common.py @@ -11,6 +11,8 @@ import os from django.conf import global_settings +import hobo.logger + DEBUG = False TEMPLATE_DEBUG = False @@ -40,7 +42,7 @@ LOGGING = { }, 'handlers': { 'syslog': { - 'level': 'INFO', + 'level': 'DEBUG', 'address': '/dev/log', 'class': 'logging.handlers.SysLogHandler', 'formatter': 'syslog', @@ -58,7 +60,8 @@ LOGGING = { 'loggers': { 'django.db': { # even when debugging seeing SQL queries is too much - 'level': 'INFO', + 'level': hobo.logger.DebugLogLevel('INFO', + django_setting='DEBUG_DB'), }, 'django.request': { 'level': 'ERROR', @@ -71,7 +74,7 @@ LOGGING = { 'propagate': False, }, '': { - 'level': 'INFO', + 'level': hobo.logger.DebugLogLevel('INFO'), 'handlers': ['syslog'], }, }, diff --git a/hobo/logger.py b/hobo/logger.py new file mode 100644 index 0000000..6b6d719 --- /dev/null +++ b/hobo/logger.py @@ -0,0 +1,22 @@ +import logging + +class DebugLogLevel(int): + def __new__(cls, level, django_setting='DEBUG'): + return super(DebugLogLevel, cls).__new__(cls, getattr(logging, level)) + + def __init__(self, level, django_setting='DEBUG'): + self.django_setting = django_setting + super(DebugLogLevel, self).__init__(getattr(logging, level)) + +class DjangoLogger(logging.getLoggerClass()): + def getEffectiveLevel(self): + level = super(DjangoLogger, self).getEffectiveLevel() + if isinstance(level, DebugLogLevel): + from django.conf import settings + debug = getattr(settings, level.django_setting, False) + if debug: + return logging.DEBUG + return level + +logging.setLoggerClass(DjangoLogger) + diff --git a/hobo/wsgi.py b/hobo/wsgi.py index 62ba97e..1365b21 100644 --- a/hobo/wsgi.py +++ b/hobo/wsgi.py @@ -10,5 +10,8 @@ https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/ import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hobo.settings") +# set the LoggerClass to DjangoLogger +import hobo.logger + from django.core.wsgi import get_wsgi_application application = get_wsgi_application() diff --git a/manage.py b/manage.py index c84d3a0..984c4e0 100755 --- a/manage.py +++ b/manage.py @@ -2,6 +2,9 @@ import os import sys +# set the LoggerClass to DjangoLogger +import hobo.logger + if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hobo.settings") -- 2.1.4