Redirecting stdout to file nodejs

前端 未结 5 1565
情歌与酒
情歌与酒 2020-12-29 06:12

I\'ve created:

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

Then piped:

process.s         


        
5条回答
  •  别那么骄傲
    2020-12-29 06:35

    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)

提交回复
热议问题