Project

General

Profile

0001-add-a-custom-logger-class-to-make-log-level-dynamic-.patch

Benjamin Dauvergne, 22 Jul 2015 05:26 PM

Download (3.63 KB)

View differences:

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
debian/debian_config_common.py
11 11
import os
12 12
from django.conf import global_settings
13 13

  
14
import hobo.logger
15

  
14 16
DEBUG = False
15 17
TEMPLATE_DEBUG = False
16 18

  
......
40 42
    },
41 43
    'handlers': {
42 44
        'syslog': {
43
            'level': 'INFO',
45
            'level': 'DEBUG',
44 46
            'address': '/dev/log',
45 47
            'class': 'logging.handlers.SysLogHandler',
46 48
            'formatter': 'syslog',
......
58 60
    'loggers': {
59 61
        'django.db': {
60 62
            # even when debugging seeing SQL queries is too much
61
            'level': 'INFO',
63
            'level': hobo.logger.DebugLogLevel('INFO',
64
                                               django_setting='DEBUG_DB'),
62 65
        },
63 66
        'django.request': {
64 67
            'level': 'ERROR',
......
71 74
            'propagate': False,
72 75
        },
73 76
        '': {
74
            'level': 'INFO',
77
            'level': hobo.logger.DebugLogLevel('INFO'),
75 78
            'handlers': ['syslog'],
76 79
        },
77 80
    },
hobo/logger.py
1
import logging
2

  
3
class DebugLogLevel(int):
4
    def __new__(cls, level, django_setting='DEBUG'):
5
        return super(DebugLogLevel, cls).__new__(cls, getattr(logging, level))
6

  
7
    def __init__(self, level, django_setting='DEBUG'):
8
        self.django_setting = django_setting
9
        super(DebugLogLevel, self).__init__(getattr(logging, level))
10

  
11
class DjangoLogger(logging.getLoggerClass()):
12
    def getEffectiveLevel(self):
13
        level = super(DjangoLogger, self).getEffectiveLevel()
14
        if isinstance(level, DebugLogLevel):
15
            from django.conf import settings
16
            debug = getattr(settings, level.django_setting, False)
17
            if debug:
18
                return logging.DEBUG
19
        return level
20

  
21
logging.setLoggerClass(DjangoLogger)
22

  
hobo/wsgi.py
10 10
import os
11 11
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hobo.settings")
12 12

  
13
# set the LoggerClass to DjangoLogger
14
import hobo.logger
15

  
13 16
from django.core.wsgi import get_wsgi_application
14 17
application = get_wsgi_application()
manage.py
2 2
import os
3 3
import sys
4 4

  
5
# set the LoggerClass to DjangoLogger
6
import hobo.logger
7

  
5 8
if __name__ == "__main__":
6 9
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hobo.settings")
7 10

  
8
-