115 lines
3.6 KiB
Python
115 lines
3.6 KiB
Python
# hobo - portal to configure and deploy applications
|
|
# Copyright (C) 2019 Entr'ouvert
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
# under the terms of the GNU Affero General Public License as published
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import datetime
|
|
import logging
|
|
import logging.config
|
|
import time
|
|
|
|
import pytest
|
|
import pytz
|
|
from tenant_schemas.utils import tenant_context
|
|
|
|
from hobo.logger import ClampLogLevel, DebugLog
|
|
|
|
|
|
@pytest.fixture
|
|
def debug_log(settings, tmpdir):
|
|
debug_log_path = str(tmpdir / 'debug.log')
|
|
settings.DEBUG_LOG_PATH = debug_log_path
|
|
|
|
LOGGING = {
|
|
'version': 1,
|
|
'disable_existing_loggers': True,
|
|
'filters': {
|
|
'debug_log': {
|
|
'()': 'hobo.logger.DebugLogFilter',
|
|
},
|
|
'request_context': {
|
|
'()': 'hobo.logger.RequestContextFilter',
|
|
},
|
|
},
|
|
'formatters': {
|
|
'debug': {
|
|
'format': settings.DEBUG_LOG_FORMAT,
|
|
},
|
|
},
|
|
'handlers': {
|
|
'debug': {
|
|
'level': 'DEBUG',
|
|
'class': 'hobo.logger.TimedRotatingFileHandler',
|
|
'formatter': 'debug',
|
|
'filename': debug_log_path,
|
|
'when': 'midnight',
|
|
'backupCount': 1,
|
|
'interval': 1,
|
|
'filters': ['request_context', 'debug_log'],
|
|
}
|
|
},
|
|
'loggers': {
|
|
'multitenant': {
|
|
'level': 'DEBUG',
|
|
'handlers': ['debug'],
|
|
},
|
|
},
|
|
}
|
|
logging.config.dictConfig(LOGGING)
|
|
|
|
logger = logging.getLogger('multitenant')
|
|
yield logger
|
|
logger.removeHandler(logger.handlers[0])
|
|
|
|
|
|
def test_debug_log(tenants, settings, app, rf, debug_log, freezer):
|
|
freezer.move_to('2020-4-20')
|
|
request = rf.get('/path/')
|
|
debug_log.info('test %s is ok', 1, extra={'request': request, 'tenant': 'yes'})
|
|
lines = list(DebugLog.lines())
|
|
assert len(lines) == 0
|
|
|
|
settings.DEBUG_LOG = True
|
|
with tenant_context(tenants[0]):
|
|
debug_log.info(
|
|
'log %s is \nok', 2, extra={'request': request, 'tenant': 'tenant1', 'user': 'jean darmette'}
|
|
)
|
|
debug_log.debug('log %s is \nok', 3, extra={'request': request})
|
|
lines = list(DebugLog.lines())
|
|
assert len(lines) == 2
|
|
request_id = hex(id(request))[2:].upper()
|
|
assert lines[0] == {
|
|
'cursor': 111,
|
|
'ip': '127.0.0.1',
|
|
'request_id': 'r:' + request_id,
|
|
'message': 'log 2 is \nok',
|
|
'level': 'INFO',
|
|
'tenant': 'tenant1.example.net',
|
|
'timestamp': pytz.timezone(time.tzname[0]).localize(datetime.datetime(2020, 4, 20, 2, 0)),
|
|
'user': '-',
|
|
'logger': 'multitenant',
|
|
}
|
|
|
|
# check that seeking by cursor gives the same lines
|
|
lines2 = list(DebugLog.lines(cursor=lines[0]['cursor']))
|
|
assert len(lines2) == 1
|
|
assert lines[1] == lines2[0]
|
|
|
|
|
|
def test_clamp_log_level(caplog):
|
|
logger = logging.getLogger('django.security.SuspiciousFileOperation')
|
|
logger.addFilter(ClampLogLevel(level='WARNING'))
|
|
logger.error('test')
|
|
assert caplog.records[-1].levelname == 'WARNING'
|