Projet

Général

Profil

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

Benjamin Dauvergne, 19 septembre 2019 18:08

Télécharger (6,29 ko)

Voir les différences:

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

  
43 43
SESSION_COOKIE_SECURE = False
44 44
CSRF_COOKIE_SECURE = False
45
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
29 29
        'handlers': ['mail_admins'],
30 30
        'propagate': True,
31 31
}
32
LOGGING['handlers']['debug']['filename'] = 'debug.log'
32
-