use a special logger to make DEBUG log activation dynamic (fixes #8028)

Adds a new logger called DjangoLogger which check if the level is a
SettingsLogLevel object, in this case it checks a Django setting to
known if it should return DEBUG intead of the default log level.

The authentic2.logger module must be imported before using the logging
module so that all created logger use our new class. To achieve that
import is done in authentic2-ctl and wsgi.py which are the entrypoints
for authentic2.
This commit is contained in:
Benjamin Dauvergne 2015-07-24 11:09:26 +02:00
parent dd35877002
commit 0bb88003fb
4 changed files with 32 additions and 10 deletions

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python
import os
import sys
import authentic2.logger
if __name__ == "__main__":
config_file = False

24
src/authentic2/logger.py Normal file
View File

@ -0,0 +1,24 @@
import logging
class SettingsLogLevel(int):
def __new__(cls, default_log_level, debug_setting='DEBUG'):
return super(SettingsLogLevel, cls).__new__(
cls, getattr(logging, default_log_level))
def __init__(self, default_log_level, debug_setting='DEBUG'):
self.debug_setting = debug_setting
super(SettingsLogLevel, self).__init__(
getattr(logging, default_log_level))
class DjangoLogger(logging.getLoggerClass()):
def getEffectiveLevel(self):
level = super(DjangoLogger, self).getEffectiveLevel()
if isinstance(level, SettingsLogLevel):
from django.conf import settings
debug = getattr(settings, level.debug_setting, False)
if debug:
return logging.DEBUG
return level
logging.setLoggerClass(DjangoLogger)

View File

@ -1,3 +1,4 @@
import logging
import logging.config
# Load default from Django
from django.conf.global_settings import *
@ -5,7 +6,7 @@ import os
import django
from . import plugins
from . import plugins, logger
BASE_DIR = os.path.dirname(__file__)
@ -16,7 +17,7 @@ BASE_DIR = os.path.dirname(__file__)
SECRET_KEY = 'please-change-me-with-a-very-long-random-string'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = False
DEBUG_DB = False
TEMPLATE_DEBUG = True
MEDIA = 'media'
@ -240,8 +241,8 @@ LOGGING = {
# even when debugging seeing SQL queries is too much, activate it
# explicitly using DEBUG_DB
'django.db': {
'level': 'INFO',
'handlers': ['console_db'],
'level': logger.SettingsLogLevel('INFO', debug_setting='DEBUG_DB'),
'propagate': False,
},
# django_select2 outputs debug message at level INFO
@ -250,7 +251,7 @@ LOGGING = {
},
'': {
'handlers': ['console'],
'level': 'INFO',
'level': logger.SettingsLogLevel('INFO'),
},
},
}
@ -274,10 +275,4 @@ REST_FRAMEWORK = {
if 'AUTHENTIC2_SETTINGS_FILE' in os.environ:
execfile(os.environ['AUTHENTIC2_SETTINGS_FILE'])
# Post local config setting
if DEBUG:
LOGGING['loggers']['']['level'] = 'DEBUG'
if DEBUG_DB:
LOGGING['loggers']['django.db']['level'] = 'DEBUG'
logging.config.dictConfig(LOGGING)

View File

@ -15,6 +15,8 @@ framework.
"""
import os
from . import logger
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "authentic2.settings")
# This application object is used by any WSGI server configured to use this