Created
June 24, 2022 15:00
-
-
Save tilleps/8bc444ea13ccdde1faf0148e7d8ad6f0 to your computer and use it in GitHub Desktop.
Log formatter
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 node | |
const EventEmitter = require("events"); | |
const stdin = new EventEmitter(); | |
stdin.on("line", console.error); | |
let buff = ""; | |
process.stdin.resume(); | |
process.once("SIGINT", function () { | |
if (buff.length > 0) { | |
processLines([buff]); | |
}; | |
process.on("SIGINT", function () { | |
process.exit(0); | |
}); | |
}); | |
process.stdin | |
.on("data", function (data) { | |
buff += data; | |
let lines = buff.split(/\r\n|\n/); | |
// Remove trailing newline | |
buff = lines.pop(); | |
processLines(lines); | |
}) | |
.on("end", function () { | |
if (buff.length > 0) { | |
processLines([buff]); | |
}; | |
}); | |
function processLines(lines) { | |
lines.map(formatToJSON).filter(logFilter).map(customFormatter).forEach(function (line) { | |
stdin.emit("line", line); | |
}); | |
} | |
function formatToJSON(line) { | |
try { | |
return JSON.parse(line); | |
} | |
catch (err) { | |
//console.error(">", err); | |
//console.error(">", line); | |
return line; | |
} | |
} | |
function logFilter(log) { | |
let filterEnabled = true; | |
// Accept all logs if no custom log filter is enabled | |
return (filterEnabled ? customLogFilter(log) : true); | |
} | |
// | |
// @todo Load from somewhere else | |
// | |
function customLogFilter(log) { | |
if (true) { | |
return true; | |
} | |
if (typeof log !== "object") { | |
return false; | |
} | |
//console.log("FILTER", log); | |
if (log.one === "first") { | |
return true; | |
} | |
return false; | |
} | |
function customFormatter(x) { | |
if (false) { | |
return `[${x.time}]`; | |
} | |
return x; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment