From 258061344d47a555aed7ee86a325b6114dbd27a0 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 16 May 2019 17:42:38 +0200 Subject: [PATCH 1/3] debian: add debug log in /var/log//debug (#29149) It's configured through the DEBUG_LOG settings: * DEBUG_LOG = False, no debug log * DEBUG_LOG = True, all debug log go to /var/log//debug * DEBUG_LOG = app1,app2, only debug log of logger app1 and app2 go to /var/log//debug /var/log//debug is emptied everyday at midnight. --- debian/debian_config_common.py | 26 +++++++++++--- hobo/logger.py | 19 ++++++++++ tests_authentic/settings.py | 2 ++ .../test_request_context_filter.py | 35 ++++++++++++++++++- tests_passerelle/settings.py | 1 + 5 files changed, 77 insertions(+), 6 deletions(-) diff --git a/debian/debian_config_common.py b/debian/debian_config_common.py index 8d92ebe..0a0ece1 100644 --- a/debian/debian_config_common.py +++ b/debian/debian_config_common.py @@ -62,26 +62,33 @@ LOGGING = { 'force_debug': { '()': 'hobo.logger.ForceDebugFilter', }, + 'debug_log': { + '()': 'hobo.logger.DebugLogFilter', + }, }, 'formatters': { 'syslog': { 'format': '%(application)s %(levelname)s %(tenant)s %(ip)s %(user)s %(request_id)s' ' %(message)s', }, + 'debug': { + 'format': '%(asctime)s %(tenant)s %(ip)s %(user)s %(request_id)s %(levelname)s %(name)s ' + ' %(message)s', + }, 'syslog_no_filter': { 'format': '%(levelname)s %(message)s', }, }, 'handlers': { 'syslog': { - 'level': 'DEBUG', + 'level': 'INFO', 'address': '/dev/log', 'class': 'logging.handlers.SysLogHandler', 'formatter': 'syslog', 'filters': ['request_context'], }, 'syslog_no_filter': { - 'level': 'DEBUG', + 'level': 'INFO', 'address': '/dev/log', 'class': 'logging.handlers.SysLogHandler', 'formatter': 'syslog_no_filter', @@ -91,6 +98,16 @@ LOGGING = { 'class': 'hobo.multitenant.log.AdminEmailHandler', 'include_html': True, }, + 'debug': { + 'level': 'DEBUG', + 'class': 'logging.handlers.TimedRotatingFileHandler', + 'formatter': 'debug', + 'filename': '/var/log/%s/debug' % PROJECT_NAME, + 'when': 'midnight', + 'backupCount': 1, + 'interval': 1, + 'filters': ['debug_log'], + } }, 'loggers': { 'django.db': { @@ -146,9 +163,8 @@ LOGGING = { 'propagate': False, }, '': { - 'level': hobo.logger.SettingsLogLevel( - default_log_level='INFO'), - 'handlers': ['syslog', 'mail_admins'], + 'level': 'DEBUG', + 'handlers': ['syslog', 'mail_admins', 'debug'], }, }, } diff --git a/hobo/logger.py b/hobo/logger.py index 08dee77..d8d9bff 100644 --- a/hobo/logger.py +++ b/hobo/logger.py @@ -126,3 +126,22 @@ class ForceDebugFilter(logging.Filter): record.levelno = logging.DEBUG record.levelname = 'DEBUG' return super(ForceDebugFilter, self).filter(record) + + +class DebugLogFilter(object): + '''Filter debug log records based on the DEBUG_LOG setting''' + + def filter(self, record): + debug_log = getattr(settings, 'DEBUG_LOG', False) + + if debug_log is False: + return False + elif debug_log is True: + return True + elif hasattr(debug_log, 'encode'): + # debug_log is a string + domains = [domain.strip() for domain in debug_log.split(',')] + return any(record.name == domain or (record.name.startswith(domain) and record.name[len(domain)] == '.') + for domain in domains) + else: + return bool(debug_log) diff --git a/tests_authentic/settings.py b/tests_authentic/settings.py index c51e0d4..ddea3bd 100644 --- a/tests_authentic/settings.py +++ b/tests_authentic/settings.py @@ -46,3 +46,5 @@ SESSION_COOKIE_SECURE = False CSRF_COOKIE_SECURE = False LANGUAGE_CODE = 'en' + +LOGGING['handlers']['debug']['filename'] = 'debug.log' diff --git a/tests_multitenant/test_request_context_filter.py b/tests_multitenant/test_request_context_filter.py index 4e00fcf..82474d9 100644 --- a/tests_multitenant/test_request_context_filter.py +++ b/tests_multitenant/test_request_context_filter.py @@ -2,7 +2,7 @@ import pytest import logging -from hobo.logger import RequestContextFilter +from hobo.logger import RequestContextFilter, DebugLogFilter from tenant_schemas.utils import tenant_context @@ -93,3 +93,36 @@ def test_systemd(settings, tenants, client, journald_handler): assert record['USER_DISPLAY_NAME'] == 'John Doe' assert record['USER_UUID'] == 'ab' * 16 assert record['APPLICATION'] == 'fake-agent' + + +def test_debug_log_filter(caplog, settings): + # default caplog log level is INFO + caplog.set_level(logging.DEBUG) + + root_logger = logging.getLogger() + assert len(root_logger.handlers) == 1 + root_logger.handlers[0].addFilter(DebugLogFilter()) + + root_logger.debug('l1') + assert 'l1' not in caplog.text + + settings.DEBUG_LOG = True + root_logger.debug('l2') + assert 'l2' in caplog.text + + settings.DEBUG_LOG = False + root_logger.debug('l3') + assert 'l3' not in caplog.text + + settings.DEBUG_LOG = 'app1,app2' + root_logger.debug('l4') + assert 'l4' not in caplog.text + + logging.getLogger('app3').debug('l5') + assert 'l5' not in caplog.text + + logging.getLogger('app1').debug('l6') + assert 'l6' in caplog.text + + logging.getLogger('app2').debug('l7') + assert 'l7' in caplog.text diff --git a/tests_passerelle/settings.py b/tests_passerelle/settings.py index d489bc6..1d6d834 100644 --- a/tests_passerelle/settings.py +++ b/tests_passerelle/settings.py @@ -31,3 +31,4 @@ LOGGING['loggers']['suds'] = { 'handlers': ['mail_admins'], 'propagate': True, } +LOGGING['handlers']['debug']['filename'] = 'debug.log' -- 2.24.0