Skip to content

Instantly share code, notes, and snippets.

@psi-4ward
Created September 1, 2022 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psi-4ward/fcbe5f804bce5d5cdcd35d732574d54c to your computer and use it in GitHub Desktop.
Save psi-4ward/fcbe5f804bce5d5cdcd35d732574d54c to your computer and use it in GitHub Desktop.
OC Log Prettifier
// deno run --allow-read json-log-view.ts log.txt
import { red, bold } from "https://deno.land/std@0.145.0/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