Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ENOTCONN with piping to process.stdout and process.stderr

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'
}
like image 751
AppDeveloper Avatar asked Nov 06 '22 16:11

AppDeveloper


1 Answers

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

like image 183
Sam Washington Avatar answered Nov 14 '22 22:11

Sam Washington