Pattern Library Utilities

getOptions

declaration
getOptions

@file Abstracted logging properties and methods.

If for any reason, they need a different logging tool or need to be extended,
the changes can be made here.

var getOptions = require('./get-options');
var util = require('util');
var winston = require('winston');

getLoggerOptions

function
getLoggerOptions()

Get options for logging system. Checks project's options against defaults, and sets up Winston's transport requirements

function getLoggerOptions() {
  'use strict';

  var defaultOptions = {
    logger: {
      level: 'info',
      silent: false,
      exitOnError: true,
      logFile: ''
    }
  };

  var loggerOptions = {};

  // get the project's options
  var projectOptions = getOptions.getProjectOptions();

  // merge project options with default options; project options take precedence
  var options = getOptions.mergeOptions(defaultOptions, projectOptions);

  // check if we're unit testing
  if (isTest()) {
    options.logger.level = 'warn';
  }

  // basic transports from options
  loggerOptions.transports = [
    new winston.transports.Console({
      level: options.logger.level,
      silent: options.logger.silent,
      colorize: true
    })
  ];

  // check if we should add a log file
  if (options.logger.logFile) {
    loggerOptions.transports.push(
      new winston.transports.File({
        filename: options.logger.logFile,
        level: options.logger.level
      })
    );
  }

  loggerOptions.exitOnError = options.logger.exitOnError;

  return loggerOptions;

}

logger

property
exports.logger

Instantiate winston

Can use metadata at any logging level

exports.logger = new winston.Logger(getLoggerOptions());

isTest

function
isTest()

Checks if this utility is called from a unit test.

function isTest() {
  'use strict';

  var isTest = false;

  for (var i = 0; i < process.argv.length; i++) {
    if (process.argv[i].substr(-5) === 'mocha') {
      isTest = true;
      break;
    }
  }

  return isTest;
}

inspectObj

method
exports.inspectObj()

@param Type Description
obj Object - The object to be inspected.
showHidden boolean - Whether or not to show hidden properties.
depth number - The number of recursion levels to traverse.

Recursively prints the properties of an object.

exports.inspectObj = function (obj, showHidden, depth) {
  'use strict';

  if (typeof obj === 'object') {
    depth = depth ? depth : 2;
    winston.info(util.inspect(obj, showHidden, depth));
  }
  else {
    winston.info(obj);
  }
};