Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended way of applying Bunyan to a large node application?

I'm working on a node application with multiple modules. I am now trying to setup logging properly (should have done so at the start), and looking at using Bunyan.

Would it be better to have a single logger module that is exported and then required by the other modules, as suggested in this answer or define a new bunyan logger instance in each module directly and configure it accordingly? For reuse I imagine the former, but I don't know if this would be restrictive going forward.

If I have a single logger defined like

var bunyan        = require('bunyan');
var logger = bunyan.createLogger({
   name: "filter",
   streams: [
      {
         level: 'info',
         stream: process.stdout
      },
      {
         level: 'error',
         path: '../error.log'
      },
      {
         level: 'debug',
         path: '../debug.log'
      }
   ]
});

module.exports = logger;

Then all the modules that make use of it would also be logging with the name filter, whereas it might make more sense for each module to log to a name that better represents itself.

Also, am I right in thinking that all modules should log errors to the same log file e.g. systemErr.log (to allow a better overview) or should they log to their own error logs, e.g. module1Err.log, module2Err.log?

like image 323
Philip O'Brien Avatar asked Oct 30 '14 10:10

Philip O'Brien


1 Answers

Would it be better to have a single logger module that is exported and then required by the other modules

Yes. The simpler, the better. Also this avoids boilerplate repetition of log setup.

Also, am I right in thinking that all modules should log errors to the same log file e.g. systemErr.log (to allow a better overview) or should they log to their own error logs, e.g. module1Err.log, module2Err.log?

One file for the whole application. Because bunyan uses ndjson format, it is easy enough to filter the main log file when you need to. I recommend the simplicity and flexibility of logging directly to stdout and allowing the deployment environment to decide where that should go. This is also convenient for development where you probably don't need or want log files on disk. Tools like upstart and multilog can properly write your stdout logs to disk and take care of log rotation for you.

One extra tip. When developing locally I run my app with something like this:

node-dev --inspect server.js |\
  tee -a log/app.ndjson.log | bunyan -o short
  • node-dev restarts automatically when I change the code
  • --inspect enables the debugger which I can attach to with chrome devtools
  • teecopies stdout to disk so if I do want to go back and look through logs in the future, I can, but I don't want to see the full ndjson records in my terminal
  • bunyan -o short gives me pretty-printed output to my terminal which is what I want for local development
like image 91
Peter Lyons Avatar answered Oct 18 '22 11:10

Peter Lyons