Created
September 1, 2022 13:42
-
-
Save psi-4ward/fcbe5f804bce5d5cdcd35d732574d54c to your computer and use it in GitHub Desktop.
OC Log Prettifier
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
// deno run --allow-read json-log-view.ts log.txt | |
import { red, bold } from "https://deno.land/[email protected]/fmt/colors.ts"; | |
const text = await Deno.readTextFile(Deno.args[0]); | |
const lines = text.split(/\r?\n/).filter(l => l.length > 0); | |
const objs = lines.map(l => { | |
const res = JSON.parse(l); | |
if (res.message.includes('{\"Exception\"')) { | |
res.message = JSON.parse(res.message.replace(/^[^{]+/, '')); | |
} | |
if (res.message?.Trace) { | |
res.message.Trace = res.message.Trace.replace('\n', "\n"); | |
} | |
return res; | |
}); | |
function printObj(obj: Record<string, string | number | Record<string, string | number>>, indent = 0) { | |
Object.entries(obj).forEach(([key, val]) => { | |
key = bold(key); | |
if (obj.level && obj.level >= 3) { | |
key = red(key); | |
} | |
if (typeof val === 'object') { | |
console.log(" ".repeat(indent) + key + ':'); | |
printObj(val, indent + 1); | |
} else { | |
let res = val; | |
if (typeof val === 'string') { | |
const lines = val.split(/\r?\n/); | |
if (lines.length === 1) { | |
res = lines[0]; | |
} else { | |
res = lines.reduce((res: string, line: string) => res += " ".repeat(indent + 1) + line + "\n", "\n"); | |
res = res.slice(0, -1); | |
} | |
} | |
console.log(" ".repeat(indent) + key + ':', res); | |
} | |
}); | |
} | |
objs.forEach(obj => { | |
printObj(obj); | |
console.log(""); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment