-
Notifications
You must be signed in to change notification settings - Fork 0
/
jlogs
executable file
·90 lines (75 loc) · 2.46 KB
/
jlogs
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env -S python3 -u
import os, sys, json, datetime
class colors:
def __init__(self):
self.cols = dict(black=30, red=31, green=32, yellow=33, blue=34, violet=35,
cyan=36, white=37, blackbg=40, redbg=41, greenbg=42, yellowbg=43,
bluebg=44, violetbg=45, cyanbg=46, whitebg=47, grey=90, red2=91,
green2=92, yellow2=93, blue2=94, violet2=95, cyan2=96, white2=97,
bold=1, ul=4, end=0)
def __getattr__(self, k):
return f'\x1b[{self.cols[k]}m'
class nocolors:
def __getattr__(self, k):
return ''
C = colors() if os.isatty(1) or os.getenv('colors') else nocolors()
col = lambda s, *cs: '' if not s else ''.join(cs) + s + C.end
short = bool(os.getenv('short'))
for line in sys.stdin:
try:
e = json.loads(line)
except json.JSONDecodeError:
print(line.rstrip())
continue
ts = e.pop('ts')
if ts[-5] in '+-': ts = ts[:-2] + ':' + ts[-2:]
ts = datetime.datetime.fromisoformat(ts.replace('Z','+00:00'))
if short:
ts = ts.strftime('%H:%M:%S.%f')[:-3]
else:
ts = ts.strftime('%m%d %H:%M:%S.%f')[:-3]
ts = col(ts, C.grey)
level = e.pop('level')
if C.black:
if level in ('warn',): level = col(level.upper(), C.black, C.yellowbg)
elif level in ('error', 'fatal'): level = col(level.upper(), C.black, C.redbg)
else: level = ''
else:
level = level[0].upper()
stacktrace = e.pop('stacktrace', None)
msg = e.pop('msg')
if msg == 'none': msg = None
e.pop('address', None) # ignore
if svc := e.pop('service', None):
svc = svc[0].upper()
svccol = {'H': C.green, 'F': C.blue, 'M': C.violet, 'W': C.cyan}.get(svc, C.yellow)
if cmp := e.pop('component', None):
if svc:
svc = svc + '/' + cmp
else:
svc = cmp
if C.black and svccol:
svc = col(svc, svccol)
elif svc:
svc = '[' + svc + ']'
lca = col(e.pop('logging-call-at', None), C.ul)
rest = ' '.join(f'{C.blue2}{k}{C.end}:{v}{C.grey},' for (k,v) in sorted(e.items()))
if rest:
rest = rest.rstrip(',')
if C.blue2:
rest = rest + C.end
else:
rest = '{' + rest + '}'
if short: level = svc = None
print(' '.join(filter(None, [ts, level, svc, lca, msg, rest])))
if stacktrace is not None:
st = stacktrace.splitlines()
sts = []
while st:
l = [st.pop(0).strip()]
while st and st[0].startswith('\t'):
l.append(st.pop(0).strip())
sts.append(l)
col1 = max(len(l[0]) for l in sts)
for l in sts:
print(" %*s %s" % (-col1, l[0], ';'.join(l[1:])))