-
Notifications
You must be signed in to change notification settings - Fork 61
/
parse-controller-logs
executable file
·67 lines (55 loc) · 1.58 KB
/
parse-controller-logs
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
67
#!/usr/bin/env python3
import json
import sys
import time
levels = {
'dpanic': '\033[91m',
'error': '\033[91m',
'warning': '\033[93m',
'diff': '\033[95m',
'key': '\033[97m',
'info': '\033[0m',
'debug': '\033[0m',
}
cancel_colour = '\033[0m'
def col_wrap(stri, collev):
return levels["key"] + stri + collev
filters = {sp[0]: sp[1] for sp in [arg.split(":", 1) for arg in sys.argv[1:]]}
def print_message(line):
ts = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(js["ts"])))
string = ""
if 'level' in js:
collev = levels[js['level']]
elif js.get('diff') != None:
collev = levels['diff']
else:
collev = cancel_colour
string += collev
app = "[%s]" % js["app"] if "app" in js else ""
env = "[%s]" % js["env"] if "env" in js else ""
string += f'[{ts}] {js["level"].upper()} {js.get("ctrl", "").upper()} {app or env} {js["msg"]} '
rms = ["msg", "ts", "level", "ctrl", "logger"]
for key in rms:
if key in js:
del(js[key])
s_key = sorted(js.keys())
string += " ".join([f"{col_wrap(k, collev)}:{js[k]}" for k in s_key])
string += cancel_colour
print(string)
for line in sys.stdin:
try:
js = json.loads(line)
except:
nline = line.strip("\n")
string = levels['error']
string += nline
string += cancel_colour
print(string)
continue
skip_flag = False
for k, v in filters.items():
if js.get(k) != v:
skip_flag = True
if not skip_flag:
print_message(js)
sys.stdout.flush()