Projet

Général

Profil

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

Benjamin Dauvergne, 24 mai 2019 11:07

Télécharger (5,56 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 ++++++++++
 .../test_request_context_filter.py            | 35 ++++++++++++++++++-
 3 files changed, 74 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',
......
94 101
            'level': 'ERROR',
95 102
            'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
96 103
        },
104
        'debug': {
105
            'level': 'DEBUG',
106
            'class': 'logging.handlers.TimedRotatingFileHandler',
107
            'formatter': 'debug',
108
            'filename': '/var/log/%s/debug' % PROJECT_NAME,
109
            'when': 'midnight',
110
            'backupCount': 1,
111
            'interval': 1,
112
            'filters': ['debug_log'],
113
        }
97 114
    },
98 115
    'loggers': {
99 116
        'django.db': {
......
155 172
            'propagate': False,
156 173
        },
157 174
        '': {
158
            'level': hobo.logger.SettingsLogLevel(
159
                default_log_level='INFO'),
160
            'handlers': ['syslog', 'mail_admins', 'sentry'],
175
            'level': 'DEBUG',
176
            'handlers': ['syslog', 'mail_admins', 'sentry', 'debug'],
161 177
        },
162 178
    },
163 179
}
hobo/logger.py
122 122
        record.levelno = logging.DEBUG
123 123
        record.levelname = 'DEBUG'
124 124
        return super(ForceDebugFilter, self).filter(record)
125

  
126

  
127
class DebugLogFilter(object):
128
    '''Filter debug log records based on the DEBUG_LOG setting'''
129

  
130
    def filter(self, record):
131
        debug_log = getattr(settings, 'DEBUG_LOG', False)
132

  
133
        if debug_log is False:
134
            return False
135
        elif debug_log is True:
136
            return True
137
        elif hasattr(debug_log, 'encode'):
138
            # debug_log is a string
139
            domains = [domain.strip() for domain in debug_log.split(',')]
140
            return any(record.name == domain or (record.name.startswith(domain) and record.name[len(domain)] == '.')
141
                       for domain in domains)
142
        else:
143
            return bool(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
96
-