Redirecting stdout to file nodejs

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;

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)