Add log JSON formatter

This commit is contained in:
Antoine Legrand 2017-02-02 00:17:25 +01:00 committed by Antoine Legrand
parent 4750d1c5ef
commit 3c99928a27
27 changed files with 402 additions and 41 deletions

46
util/log.py Normal file
View file

@ -0,0 +1,46 @@
import os
def logfile_path(jsonfmt=False, debug=False):
"""
Returns the a logfileconf path following this rules:
- conf/logging_debug_json.conf # jsonfmt=true, debug=true
- conf/logging_json.conf # jsonfmt=true, debug=false
- conf/logging_debug.conf # jsonfmt=false, debug=true
- conf/logging.conf # jsonfmt=false, debug=false
Can be parametrized via envvars: JSONLOG=true, DEBUGLOG=true
"""
_json = ""
_debug = ""
if jsonfmt or os.getenv('JSONLOG', 'false').lower() == 'true':
_json = "_json"
if debug or os.getenv('DEBUGLOG', 'false').lower() == 'true':
_debug = "_debug"
return 'conf/logging%s%s.conf' % (_debug, _json)
def filter_logs(values, filtered_fields):
"""
Takes a dict and a list of keys to filter.
eg:
with filtered_fields:
[{'key': ['k1', k2'], 'fn': lambda x: 'filtered'}]
and values:
{'k1': {'k2': 'some-secret'}, 'k3': 'some-value'}
the returned dict is:
{'k1': {k2: 'filtered'}, 'k3': 'some-value'}
"""
for field in filtered_fields:
cdict = values
for key in field['key'][:-1]:
if key in cdict:
cdict = cdict[key]
last_key = field['key'][-1]
if last_key in cdict and cdict[last_key]:
cdict[last_key] = field['fn'](cdict[last_key])

View file

@ -0,0 +1,41 @@
import pytest
from util.log import logfile_path, filter_logs
from app import FILTERED_VALUES
def test_filter_logs():
values = {
'user': {
'password': "toto"
},
'blob': '1234567890asdfewkqresfdsfewfdsfd',
'unfiltered': 'foo'
}
filter_logs(values, FILTERED_VALUES)
assert values == {'user': {'password': '[FILTERED]'}, 'blob': '12345678', 'unfiltered': "foo"}
@pytest.mark.parametrize('debug,jsonfmt,expected', [
(False, False, "conf/logging.conf"),
(False, True, "conf/logging_json.conf"),
(True, False, "conf/logging_debug.conf"),
(True, True, "conf/logging_debug_json.conf"),
])
def test_logfile_path(debug, jsonfmt, expected, monkeypatch):
assert logfile_path(jsonfmt=jsonfmt, debug=debug) == expected
@pytest.mark.parametrize('debug,jsonfmt,expected', [
("false", "false", "conf/logging.conf"),
("false", "true", "conf/logging_json.conf"),
("true", "false", "conf/logging_debug.conf"),
("true", "true", "conf/logging_debug_json.conf"),
])
def test_logfile_path_env(debug, jsonfmt, expected, monkeypatch):
monkeypatch.setenv("DEBUGLOG", debug)
monkeypatch.setenv("JSONLOG", jsonfmt)
assert logfile_path() == expected
def test_logfile_path_default():
assert logfile_path() == "conf/logging.conf"