I solved this problem the following way:
var access = fs.createWriteStream('/var/log/node/api.access.log');
process.stdout.write = process.stderr.write = access.write.bind(access);
Of course you can also separate stdout and stderr if you want.
I also would strongly recommend to handle uncaught exceptions:
process.on('uncaughtException', function(err) {
console.error((err && err.stack) ? err.stack : err);
});
This will cover the following situations:
- process.stdout.write
- process.stderr.write
- console.log
- console.dir
- console.error
- someStream.pipe(process.stdout);
- throw new Error(‘Crash’);
- throw ‘never do this’;
- throw undefined;