Last active
September 22, 2018 16:21
-
-
Save scaleflake/ad2a9b7c94635fee07d0414440dc8ecb to your computer and use it in GitHub Desktop.
Node.js stdout and stderr redirecting to file.
This file contains 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 | |
/* jshint esversion: 6*/ | |
function redirectStdout(logFilePath) { | |
const fs = require('fs'); | |
let access = fs.createWriteStream(logFilePath, { | |
'flags': 'a', | |
'encoding': null, | |
'mode': 0o666 | |
}); | |
process.stdout.write = () => { | |
let now = Date.now(); | |
for (let i = 0; i < arguments.length - 1; i++) { | |
let lines = arguments[i].split('\n'); | |
for (let j = 0; j < lines.length - 1; j++) { | |
access.write(`${now} LOG: ${lines[j]}\n`); | |
} | |
} | |
}; | |
process.stderr.write = () => { | |
let now = Date.now(); | |
for (let i = 0; i < arguments.length - 1; i++) { | |
let lines = arguments[i].split('\n'); | |
for (let j = 0; j < lines.length - 1; j++) { | |
access.write(`${now} ERR: ${lines[j]}\n`); | |
} | |
} | |
}; | |
process.on('uncaughtException', function(err) { | |
console.error((err && err.stack) ? err.stack : err); | |
}); | |
} | |
redirectStdout(fs, '/darcy/testlog'); | |
console.log({ a: 'sadasd', l: '90900' }); | |
console.log([1, 3, 7, 8]); | |
console.log(() => {}); | |
console.log('sadasdasdasdasd'); | |
console.log(`11dfsfsdfsfddsfsdf\n22dasasfsdfas\n33adfsdfsafd`); | |
console.log(JSON.stringify({ requir: 'qirwporiqwp', consol: '73291873918' }, null, 3)); | |
/* | |
1528999200035 LOG: { a: 'sadasd', l: '90900' } | |
1528999200037 LOG: [ 1, 3, 7, 8 ] | |
1528999200037 LOG: [Function] | |
1528999200037 LOG: sadasdasdasdasd | |
1528999200037 LOG: 11dfsfsdfsfddsfsdf | |
1528999200037 LOG: 22dasasfsdfas | |
1528999200037 LOG: 33adfsdfsafd | |
1528999200037 LOG: { | |
1528999200037 LOG: "requir": "qirwporiqwp", | |
1528999200037 LOG: "consol": "73291873918" | |
1528999200037 LOG: } | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment