Created
December 24, 2020 11:28
-
-
Save wilensky/879889b1ee4382afc549c776947a2d48 to your computer and use it in GitHub Desktop.
NodeJS raw process output JSONifying (processes line-by-line)
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
// Usage: ~$ some-process | node output_jsonify.js | |
const pump = require('pump'); | |
const split = require('split2'); | |
const through = require('through2'); | |
const dope = msg => ({ | |
pid: process.pid, | |
time: new Date().getTime(), | |
level: /Error/.test(msg) ? 3 : 30, | |
msg | |
}); | |
pump( | |
process.stdin, | |
split(), | |
through((chunk, enc, cb) => { | |
const msg = chunk.toString(); | |
// Avoiding stack trace | |
if (/^\s+/.test(msg) || msg.indexOf('}') !== -1) { | |
return cb(); | |
} | |
console.log(JSON.stringify(dope(msg))); | |
cb(); | |
}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment