How do I use npm forever-monitor to log to stdout

759 Views Asked by At

I have a simple nodejs docker service. I'm watching stdout in development and logging to AWS cloudwatch in production.

I've just added forever-monitor, but that breaks my logging. So I've started catching stdout on the child process,

const forever = require('forever-monitor');

const child = new (forever.Monitor)('server.js', {
  max: 3,
  silent: true,
  args: []
});

child.on('stdout', function(data) {
  console.log(data);
});

but that just gives me byte code out -

[nodemon] starting `node forever.js`
<Buffer 63 6f 6e 6e 65 63 74 65 64 20 74 6f 20 6c 69 76 65 20 64 62 0a>

How do I get my console.log statements back into std-out?

1

There are 1 best solutions below

0
Aidan Ewen On

It looks like data is a stream (see node docs).

I've updated my code to -

child.on('stdout', function(data) {
  console.log(data.toString());
});

And now it's working as expected. (I found this question useful).