Redirecting stdout to file nodejs

2019-03-12 21:35发布

I've created:

var access = fs.createWriteStream('/var/log/node/api.access.log', { flags: 'w' });

Then piped:

process.stdout.pipe(access);

Then tried:

console.log("test");

And nothing has appeared in /var/log/node/api.access.log. However this way is working:

process.stdout.pipe(access).write('test');

Could someone explain what am I doing wrong ?

4条回答
趁早两清
2楼-- · 2019-03-12 22:15

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;
查看更多
手持菜刀,她持情操
3楼-- · 2019-03-12 22:18

process.stdout is a Writable. pipe is a method of Readable(Cf StreamAPI documentation : https://nodejs.org/api/stream.html

You can see the documentation of process.stdout here : https://nodejs.org/api/process.html#process_process_stdout

It's surprising that you can do process.stdout.pipe(...); without any error. But i suppose this call just do nothing. Except returning a new Writable stream binded to stdout (or maybe it returns process.stdout itself. There's no specification for that in the documentation).

If you want to redirect stdout to a file, you have many solutions :

  • Just use your command line to do that. Windows style : node myfile.js > api.access.log.
  • Replace the console object by your own object. And you can rewrite console methods.
  • I'm not sure, but it may be possible to replace process.stdout with your own stream (and you can do whatever you want with this)
查看更多
劳资没心,怎么记你
4楼-- · 2019-03-12 22:18

@user3173842 for the reply on 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);

you do understand that process.stdout continues after process.on('exit') and therefore the fs.WriteStream closes after with process.stdout, according to https://github.com/nodejs/node/issues/7606

so now the question remains, if the developer desired to have the fs.Writestream.write() return to its normal functionality and when fs.Writestream.end is called the writestream closes. How would the developer go about doing this I did

a_l = asyncify_listener
p_std_stream_m is a process stream manager object

p_std_stream_m.std_info.p_stdout_write = process.stdout.write   

process.stdout.write = w_stream.write.bind(w_stream)

process.once('beforeExit', a_l(   p_std_stream_m.handler,process.stdout,w_stream   )   )       

     where in the 'beforeExit' event listener I did
process.stdout.write = p_std_stream_m.std_info.p_stdout_write

w_stream.end()

It works and you use the once method because the process.stdout seems to do a lot of work at this time. Is this good practice, would you do this or what would you do in this situation anyone can feel free to reply.

查看更多
相关推荐>>
5楼-- · 2019-03-12 22:27

Checkout console.Console, the parent class of the normal console.

var myLogFileStream = fs.createWriteStream(pathToMyLogFile);

var myConsole = new console.Console(myLogFileStream, myLogFileStream);

You can then you use myConsole.log, myConsole.error, myConsole.dir, etc. and write directly to your file.

You can also monkey patch process.stdout.write as follows:

var fn = process.stdout.write;

function write() {
  fn.apply(process.stdout, arguments);
  myLogFileStream.write.apply(myLogFileStream, arguments);
}

process.stdout.write = write;

there are also other options for overwriting console._stdout depending on the motivation for logging the stdout to a file.

查看更多
登录 后发表回答