Disabling Bunyan in tests

Bunyan (as of v1.8.10) doesn’t provide an explicit api to mute logging. This is especially useful when you’re running unit tests and don’t want logs and test reports to be mixed.

One workaround suggested by the author is to set the log level to a value above FATAL.

Set an environment variable when running tests:

// package.json
...
...
  "scripts": {
    "test": "NODE_ENV=test mocha"
  }
...
...

Check for it in the logger module and if it matches, set the log level above FATAL.

// logger.js
const bunyan = require("bunyan");
const logger = bunyan.createLogger({name: "myapp"});

if (process.env.NODE_ENV === "test") {
  logger.level(bunyan.FATAL + 1);
}

module.exports = logger;

This disables logging when running tests.