0

Im not a node developer by nature and I have this project and I am trying to debug something. And I notice that the logger is logging to process.stdout .... How can I tail -f this in a running node server?

slipperypete
  • 5,358
  • 17
  • 59
  • 99

1 Answers1

0

Option #1 - Most common

Just run your script/server like this:

node ./my-server.js >/path/to/logs/my-server.log 2>&1

Then to tail ...

tail -f /path/to/logs/my-server.log

NB.: For production, you might want to use something like logrotate in addition.

Option #2 - For hackers

Wherever you are using process.stdout.write(msg) or console.log(msg), you can use process.emit('logger#receiver-id', JSON.stringify({content: msg}))

// sample.js
function doSomething() {
  // console.log('Hello world');
  var msg = 'Hello World !';
  process.emit('logger#catch-all', JSON.stringify({content: msg}))
}
doSomething();
// tail.js
function onPayload(payload) {
  var msg = JSON.parse(payload).content;
  process.stdout.write(msg);
}

function registerEvent() {
  process.on('logger#catch-all', onPayload);
}
registerEvent();

Then you can now get your logs by running (first) tail.js, then sample.js

This method is just a sample of what you can do by considering logs as events ... You can then write your own containers and format payloads depending on where your logs are going: stdout, file, monitoring tools ...

You might have to override console.<method>() if you do not want to use directly process.emit(...).