-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-log
66 lines (53 loc) · 1.9 KB
/
json-log
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python3
import sys
import json
import argparse
from jinja2 import Environment, FileSystemLoader
FIELDS = '@fields'
DEFAULT_TEMPLATE = '[{{timestamp}}] {{fields.level}} {{message}}'
def make_log_filter(condition=None):
if condition is not None:
split_condition = condition.split('=')
field = split_condition[0].split('.')[1]
value = split_condition[1]
condition = {field: value}
else:
condition = {}
def filter(entry):
for k, v in condition.items():
if entry[FIELDS][k] not in v:
return False
return True
return filter
def make_formatter(template_file=None):
if template_file is not None:
env = Environment(loader=FileSystemLoader(searchpath='/'))
template = env.get_template(template_file)
else:
template = Environment().from_string(DEFAULT_TEMPLATE)
def render(entry):
for k, v in entry.items():
if k.startswith('@'):
del entry[k]
entry[k.lstrip('@')] = v
return template.render(**entry)
return render
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Read from stdin and filter json-like log')
parser.add_argument('--filter', dest='condition', help='Filter condition')
parser.add_argument('--template', dest='template', help='Formatting template.')
args = parser.parse_args()
line_filter = make_log_filter(args.condition)
formatter = make_formatter(args.template)
try:
for line in map(formatter, filter(line_filter, map(json.loads, sys.stdin))):
print(line)
except KeyboardInterrupt:
print('Stopped')
sys.exit(0)
except ValueError:
print('Log entry is not valid json')
sys.exit(1)
except IndexError:
print('Wrong format of filter. Please use @fields.your_field=your_value')
sys.exit(1)