Created
October 13, 2023 19:26
-
-
Save brianv0/102473ca4f04b90381aa93cfbb1dbba8 to your computer and use it in GitHub Desktop.
jog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from signal import signal, SIGPIPE, SIG_DFL | |
signal(SIGPIPE,SIG_DFL) | |
import json | |
import sys | |
tty = True # sys.stdout.isatty() | |
continue_lines = [] | |
max_continue = 10 | |
last_line = None | |
while True: | |
line_in = sys.stdin.readline() | |
if not len(line_in): | |
break | |
line = line_in.rstrip("\n") | |
if not len(line.strip()): | |
continue | |
if len(continue_lines): | |
if line.strip()[0] == "{": | |
print(">>> " + "\n>>> ".join(continue_lines)) | |
continue_lines = [] | |
else: | |
line = "".join(continue_lines) + line | |
if line.strip()[0] != "{": | |
print(line) | |
continue | |
try: | |
data = json.loads(line.strip()) | |
continue_lines = [] | |
except: | |
continue_lines.append(line_in) | |
if len(continue_lines) < max_continue: | |
continue | |
else: | |
print(">>> " + "\n>>> ".join(continue_lines)) | |
print(">>> " + line_in) | |
continue_lines = [] | |
continue | |
if "@timestamp" not in data: | |
print(line) | |
continue | |
thrown = "" | |
def f_exc(exc, prefix="Exception in thread",lvl=0): | |
ind = lvl*" " | |
name = exc["name"] | |
msg = name + (": " + exc["message"] if "message" in exc else "") | |
thrown = '\n{ind}{prefix} \"{data[thread]}\" {msg}\n'.format(data=data, msg=msg, prefix=prefix, ind=ind) | |
ext = [] | |
for e in exc.get("extendedStackTrace", []): | |
file = e.get("file","?") | |
ext.append('{ind} at {e[class]}::{e[method]}({file}:{e[line]})'.format(e=e,file=file,ind=ind)) | |
thrown += "\n".join(ext) | |
if "cause" in exc: | |
thrown += f_exc(exc["cause"], "Cause", lvl +1) | |
return thrown | |
if "thrown" in data: | |
thrown = f_exc(data["thrown"]) | |
if data["level"] == "ERROR": | |
color = "31" | |
if data["level"] == "WARN": | |
color = "33" | |
if data["level"] == "INFO": | |
color = "34" | |
if data["level"] == "DEBUG": | |
color = "35" | |
colors = "\x1b[1;{color}m".format(color=color) if tty else "" | |
colore = "\x1b[0m" if tty else "" | |
print('[{data[@timestamp]}] - ({data[hostname]}:{data[thread]}) {data[loggerName]}\n{colors}{data[level]}{colore}: {data[message]} {thrown}'.format(data=data, thrown=thrown, colors=colors, colore=colore)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment