Projet

Général

Profil

0001-debian-add-debug-log-in-var-log-app-debug-29149.patch

Benjamin Dauvergne, 13 avril 2020 18:40

Télécharger (6,29 ko)

Voir les différences:

Subject: [PATCH 1/3] debian: add debug log in /var/log/<app>/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/<app>/debug
* DEBUG_LOG = app1,app2, only debug log of logger app1 and app2
  go to /var/log/<app>/debug

/var/log/<app>/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(-)
debian/debian_config_common.py
62 62
        'force_debug': {
63 63
            '()': 'hobo.logger.ForceDebugFilter',
64 64
        },
65
        'debug_log': {
66
            '()': 'hobo.logger.DebugLogFilter',
67
        },
65 68
    },
66 69
    'formatters': {
67 70
        'syslog': {
68 71
            'format': '%(application)s %(levelname)s %(tenant)s %(ip)s %(user)s %(request_id)s'
69 72
                      ' %(message)s',
70 73
        },
74
        'debug': {
75
            'format': '%(asctime)s %(tenant)s %(ip)s %(user)s %(request_id)s %(levelname)s %(name)s '
76
                      ' %(message)s',
77
        },
71 78
        'syslog_no_filter': {
72 79
            'format': '%(levelname)s %(message)s',
73 80
        },
74 81
    },
75 82
    'handlers': {
76 83
        'syslog': {
77
            'level': 'DEBUG',
84
            'level': 'INFO',
78 85
            'address': '/dev/log',
79 86
            'class': 'logging.handlers.SysLogHandler',
80 87
            'formatter': 'syslog',
81 88
            'filters': ['request_context'],
82 89
        },
83 90
        'syslog_no_filter': {
84
            'level': 'DEBUG',
91
            'level': 'INFO',
85 92
            'address': '/dev/log',
86 93
            'class': 'logging.handlers.SysLogHandler',
87 94
            'formatter': 'syslog_no_filter',
......
91 98
            'class': 'hobo.multitenant.log.AdminEmailHandler',
92 99
            'include_html': True,
93 100
        },
101
        'debug': {
102
            'level': 'DEBUG',
103
            'class': 'logging.handlers.TimedRotatingFileHandler',
104
            'formatter': 'debug',
105
            'filename': '/var/log/%s/debug' % PROJECT_NAME,
106
            'when': 'midnight',
107
            'backupCount': 1,
108
            'interval': 1,
109
            'filters': ['debug_log'],
110
        }
94 111
    },
95 112
    'loggers': {
96 113
        'django.db': {
......
146 163
            'propagate': False,
147 164
        },
148 165
        '': {
149
            'level': hobo.logger.SettingsLogLevel(
150
                default_log_level='INFO'),
151
            'handlers': ['syslog', 'mail_admins'],
166
            'level': 'DEBUG',
167
            'handlers': ['syslog', 'mail_admins', 'debug'],
152 168
        },
153 169
    },
154 170
}
hobo/logger.py
126 126
        record.levelno = logging.DEBUG
127 127
        record.levelname = 'DEBUG'
128 128
        return super(ForceDebugFilter, self).filter(record)
129

  
130

  
131
class DebugLogFilter(object):
132
    '''Filter debug log records based on the DEBUG_LOG setting'''
133

  
134
    def filter(self, record):
135
        debug_log = getattr(settings, 'DEBUG_LOG', False)
136

  
137
        if debug_log is False:
138
            return False
139
        elif debug_log is True:
140
            return True
141
        elif hasattr(debug_log, 'encode'):
142
            # debug_log is a string
143
            domains = [domain.strip() for domain in debug_log.split(',')]
144
            return any(record.name == domain or (record.name.startswith(domain) and record.name[len(domain)] == '.')
145
                       for domain in domains)
146
        else:
147
            return bool(debug_log)
tests_authentic/settings.py
46 46
CSRF_COOKIE_SECURE = False
47 47

  
48 48
LANGUAGE_CODE = 'en'
49

  
50
LOGGING['handlers']['debug']['filename'] = 'debug.log'
tests_multitenant/test_request_context_filter.py
2 2

  
3 3
import logging
4 4

  
5
from hobo.logger import RequestContextFilter
5
from hobo.logger import RequestContextFilter, DebugLogFilter
6 6

  
7 7
from tenant_schemas.utils import tenant_context
8 8

  
......
93 93
        assert record['USER_DISPLAY_NAME'] == 'John Doe'
94 94
        assert record['USER_UUID'] == 'ab' * 16
95 95
        assert record['APPLICATION'] == 'fake-agent'
96

  
97

  
98
def test_debug_log_filter(caplog, settings):
99
    # default caplog log level is INFO
100
    caplog.set_level(logging.DEBUG)
101

  
102
    root_logger = logging.getLogger()
103
    assert len(root_logger.handlers) == 1
104
    root_logger.handlers[0].addFilter(DebugLogFilter())
105

  
106
    root_logger.debug('l1')
107
    assert 'l1' not in caplog.text
108

  
109
    settings.DEBUG_LOG = True
110
    root_logger.debug('l2')
111
    assert 'l2' in caplog.text
112

  
113
    settings.DEBUG_LOG = False
114
    root_logger.debug('l3')
115
    assert 'l3' not in caplog.text
116

  
117
    settings.DEBUG_LOG = 'app1,app2'
118
    root_logger.debug('l4')
119
    assert 'l4' not in caplog.text
120

  
121
    logging.getLogger('app3').debug('l5')
122
    assert 'l5' not in caplog.text
123

  
124
    logging.getLogger('app1').debug('l6')
125
    assert 'l6' in caplog.text
126

  
127
    logging.getLogger('app2').debug('l7')
128
    assert 'l7' in caplog.text
tests_passerelle/settings.py
31 31
        'handlers': ['mail_admins'],
32 32
        'propagate': True,
33 33
}
34
LOGGING['handlers']['debug']['filename'] = 'debug.log'
34
-