All files / src/utils Logger.js

100% Statements 19/19
100% Branches 8/8
100% Functions 1/1
100% Lines 19/19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50    1x 1x 1x 1x     34x 34x 34x   34x     34x 5x             34x 5x       34x 10x 2x 2x 1x                   34x           1x  
'use strict';
 
const bunyan = require('bunyan');
const path = require('path');
const fs = require('fs');
const PrettyLogStream = require('./PrettyLogStream');
 
function initLogger({Configuration}) {
  const environment = Configuration.get('env');
  const loggerConfig = Configuration.get('logs');
  const silent = Configuration.get('silent');
 
  const streams = []
 
  // If Mockiji is not in silent mode, add a stdout stream
  if (!silent) {
    streams.push({
      stream: new PrettyLogStream(),
      level: (environment === 'dev') ? 'debug' : 'info',
    });
  }
 
  // Create log streams based on the configuration
  for (let stream of loggerConfig) {
    streams.push(stream);
  }
 
  // Create logs directory for each stream if needed
  for (let stream of streams) {
    if (stream.path) {
      const logsDirectory = path.dirname(stream.path);
      if (!fs.existsSync(logsDirectory)) {
        fs.mkdirSync(logsDirectory);
      }
    }
  }
 
  // Return bunyan object so it can directly be used
  // to log things.
  //
  //   Logger.log('foo');
  //
  return bunyan.createLogger({
    name: 'mockiji',
    streams
  });
}
 
module.exports = initLogger;