Reputation: 5210
I'm looking for a way to grab all process.stdout
and pipe to a log file, I have the following:
import fs from 'fs';
let logFile = fs.createWriteStream('./test.log', {
encoding: 'utf8',
flags : 'a'
});
process.stdout.pipe(logFile);
However, it seems to still spit all stdout
to the console and I'm not getting anything in the log file (although it's being created).
Upvotes: 2
Views: 4395
Reputation: 7449
I'd rather have the program only write to stdout
and have the process that calls node decide what to do with the output:
node server | tee test.log
This has a few benefits:
And avoids a very serious drawback:
Upvotes: 4
Reputation: 1995
I think this isn't working because process.stdout
is a write stream that can't be piped from, though I'm not completely sure. What I did get working was the following example, where I override stdout with a new writable stream that has a _write
method that calls forward to the file stream's write
:
var fs = require('fs');
var writeStream = fs.createWriteStream('./test.log', {
encoding: 'utf8',
flags: 'w'
});
process.stdout = require('stream').Writable();
process.stdout._write = function(chunk, encoding, callback) {
writeStream.write(chunk, encoding, callback);
};
console.log('test from console.log');
Upvotes: 0