On a recent customer engagement myself and Red Hat Mobile consultants needed to write a logging solution that could be used across a number of Node.js microservices. It had to use a standard logging module, but provide sensible defaults too depending on what environment our Node.js code was running in. We created fh-bunyan to address this need.

fh-bunyan exposes an interface that returns standard bunyan logger instances, but it will configure them using sensible defaults depending on the environment your application is running in. Environment in this instance refers to the value of process.env.FH_ENV. Here are the default levels:

  • dev - trace
  • test - debug
  • preprod - info
  • prod - info

These can even be reconfigured by using the setLogMappings(map) function too, so if you don’t like the defaults then that’s fine! You could do the following to configure them to your liking:

var bunyan = require('fh-bunyan');

bunyan.setLogMappings({
  dev: 'trace',
  test: 'info',
  preprod: 'info',
  prod: 'warn'
});

var log = bunyan.getLogger(__filename);

log.debug('debug level - only appears in dev due to our mappings!');
log.info('info level - appears in dev, test, preprod, and prod!');

Now you can write more code, and worry less about choosing a log library and configuring it across all of your Red Hat Mobile MBaaS environments!