<commit_before>/**
 * @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 merge = require('lodash.merge');
var util = require('util');
var winston = require('winston');

/**
 * Abstracts fully-fleshed logging module to the logger property of this module.
 *
 * If logging with metadata, or requiring winston.log's more advanced features,
 * use exports.logger.log() instead of exports.log().
 * @example
 *   exports.logger(options).log('info', 'Test Log Message', { anything: 'This is metadata' });
 *
 * @param {Object} options - Configuration options.
 * @return {Object} An instance of Winston.
 */
exports.logger = function (options) {
  'use strict';

  options = getOptions(options);

  var exitOnError = options.logger.exitOnError;
  var level = options.logger.level;
  var logFile = options.logger.logFile;
  var silent = options.logger.silent;
  var transports = [
    new winston.transports.Console({
      level: level,
      silent: silent,
      colorize: true
    })
  ];

  if (logFile) {
    transports.push(
      new winston.transports.File({
        filename: logFile,
        level: level
      })
    );
  }

  var logger = new winston.Logger({
    transports: transports,
    exitOnError: exitOnError
  });

  return logger;
};

/**
 * Checks if this utility is called from a unit test.
 *
 * @return {boolean} True or false.
 */
exports.isTest = function () {
  '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;
};

/**
 * Recursively prints the properties of an object.
 *
 * @param {Object} obj - The object to be inspected.
 * @param {boolean} showHidden - Whether or not to show hidden properties.
 * @param {number} depth - The number of recursion levels to traverse.
 */
exports.inspect = function (obj, showHidden, depth) {
  'use strict';

  depth = depth ? depth : 2;
  winston.info(util.inspect(obj, showHidden, depth));
};

/**
 * The lazy dev's default method for this utility.
 *
 * Should be a drop-in replacement for console.log, although it can abstract
 * something more extensible like Winston. This logs at the info level. If run
 * from a unit test, this will not output anything.
 * Accepts the same parameters as the unabstracted tool's info() method.
 */
exports.log = exports.isTest() ? function () { 'use strict'; } : winston.info;

/**
 * @function info
 * Log at the info level.
 *
 * Accepts the same parameters as the unabstracted tool's info() method.
 */
exports.info = winston.info;

/**
 * @function warn
 * Log at the warn level.
 *
 * Accepts the same parameters as the unabstracted tool's warn() method.
 */
exports.warn = winston.warn;

/**
 * @function error
 * Log at the error level.
 *
 * Accepts the same parameters as the unabstracted tool's error() method.
 */
exports.error = winston.error;

var getOptions = function (options) {
  'use strict';

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

  /* merge project and default options */
  options = merge(optionsDefaults, options, function (a, b) {
    return Array.isArray(a) ? b : undefined;
  });

  return options;
};<commit_msg>using more standard getDefaultOptions method in logger.js<commit_after>/**
 * @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 util = require('util');
var utility = require('./utility');
var winston = require('winston');

/**
 * Function to get default options for an implementation of patternlab-import
 * @return {Object}  options  an object of default patternlab-import options
 */
var getDefaultOptions = function (options) {
  'use strict';

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

  return options;
};

/**
 * Abstracts fully-fleshed logging module to the logger property of this module.
 *
 * If logging with metadata, or requiring winston.log's more advanced features,
 * use exports.logger.log() instead of exports.log().
 * @example
 *   exports.logger(options).log('info', 'Test Log Message', { anything: 'This is metadata' });
 *
 * @param {Object} options - Configuration options.
 * @return {Object} An instance of Winston.
 */
exports.logger = function (projectOptions) {
  'use strict';

  var options = utility.getOptions(getDefaultOptions(), projectOptions);

  var exitOnError = options.logger.exitOnError;
  var level = options.logger.level;
  var logFile = options.logger.logFile;
  var silent = options.logger.silent;
  var transports = [
    new winston.transports.Console({
      level: level,
      silent: silent,
      colorize: true
    })
  ];

  if (logFile) {
    transports.push(
      new winston.transports.File({
        filename: logFile,
        level: level
      })
    );
  }

  var logger = new winston.Logger({
    transports: transports,
    exitOnError: exitOnError
  });

  return logger;
};

/**
 * Checks if this utility is called from a unit test.
 *
 * @return {boolean} True or false.
 */
exports.isTest = function () {
  '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;
};

/**
 * Recursively prints the properties of an object.
 *
 * @param {Object} obj - The object to be inspected.
 * @param {boolean} showHidden - Whether or not to show hidden properties.
 * @param {number} depth - The number of recursion levels to traverse.
 */
exports.inspect = function (obj, showHidden, depth) {
  'use strict';

  depth = depth ? depth : 2;
  winston.info(util.inspect(obj, showHidden, depth));
};

/**
 * The lazy dev's default method for this utility.
 *
 * Should be a drop-in replacement for console.log, although it can abstract
 * something more extensible like Winston. This logs at the info level. If run
 * from a unit test, this will not output anything.
 * Accepts the same parameters as the unabstracted tool's info() method.
 */
exports.log = exports.isTest() ? function () { 'use strict'; } : winston.info;

/**
 * @function info
 * Log at the info level.
 *
 * Accepts the same parameters as the unabstracted tool's info() method.
 */
exports.info = winston.info;

/**
 * @function warn
 * Log at the warn level.
 *
 * Accepts the same parameters as the unabstracted tool's warn() method.
 */
exports.warn = winston.warn;

/**
 * @function error
 * Log at the error level.
 *
 * Accepts the same parameters as the unabstracted tool's error() method.
 */
exports.error = winston.error;
{'fraction_non_alphanumeric': 0.12062987575845131, 'fraction_numerical': 0.0008668015024559376, 'mean_word_length': 3.160456730769231, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4687506', 'n_tokens_mistral': 2091, 'n_tokens_neox': 2036, 'n_words': 740}