Skip to content

Instantly share code, notes, and snippets.

@wilensky
Created December 24, 2020 11:28
Show Gist options
  • Save wilensky/879889b1ee4382afc549c776947a2d48 to your computer and use it in GitHub Desktop.
Save wilensky/879889b1ee4382afc549c776947a2d48 to your computer and use it in GitHub Desktop.
NodeJS raw process output JSONifying (processes line-by-line)
// 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