Redirecting stdout to file nodejs

前端 未结 5 1645
情歌与酒
情歌与酒 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:52

    @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.

提交回复
热议问题