Created an access and error pipe or a writable stream for console.log or console.error to be written to a file stream.
var fs = require("fs");
var access = fs.createWriteStream('/node.access.log', { flags: 'a' });
var error = fs.createWriteStream('/node.error.log', { flags: 'a' });
process.stdout.pipe(access); // redirect stdout / stderr
process.stderr.pipe(error);
my expectation is whenever I do console.log
or console.error
it's written to the file output stream.
I'm having an error
Error: read ENOTCONN
at Socket._read (net.js:528:20)
at Socket.Readable.read (_stream_readable.js:457:10)
at resume_ (_stream_readable.js:936:12)
at processTicksAndRejections (internal/process/task_queues.js:84:9)
Emitted 'error' event at:
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at processTicksAndRejections (internal/process/task_queues.js:84:9) {
errno: 'ENOTCONN',
code: 'ENOTCONN',
syscall: 'read'
}
Its because process.stdout is a writable stream, not a readable (or duplex), so its impossible to bind to its events.
You can override globally process.stdout with a duplex stream (after you are keeping the original stream in another global variable, otherwise you will need to write to file descriptor IO, using net
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With