<commit_before>/**
 * Module Dependencies
 */
var winston = require('winston');
var _ = require('lodash');

/**
 * Initialize a new `Application`.
 *
 * @api public
 */

function Logger(namespace) {
  "use strict";
  if (!(this instanceof Logger)) {
    return new Logger(namespace);
  }

  if (typeof namespace === 'undefined') {
    namespace = 'app';
  }

  if (typeof loggers[namespace] !== 'undefined') {
    return loggers[namespace];
  }

  this.namespace = namespace;

  Logger.addNamespace(namespace, this);

  setUpLogger();

  this.log = this.log.bind(this);
}

Logger.prototype.log = function() {
  var args = Array.prototype.slice.call(arguments);
  args.unshift(this.namespace);

  logger.log.apply(logger, args);
};

/**
 * Private Variables
 */
var logger = null;
var transports = [
  new (winston.transports.Console)({
    colorize: true,
    prettyPrint: true
  })
];
var loggers = {};
var lastIndex = 0;
var colors = [
  'cyan',
  'green',
  'blue',
  'magenta',
  'yellow',
  'red'
];
var maxLevel = -1;
var firstNamespace = '';
var logLevels = {};
var logColors = {};

function setUpLogger() {
  logger = new (winston.Logger)({
    transports: transports,
    levels: logLevels,
    colors: logColors
  });

  logger.level = firstNamespace;

  _.each(logger.transports, function(transport) {
    transport.level = firstNamespace;
  });
}

Logger.addTransport = function(transport) {
  transports.push(transport);
  setUpLogger();
};

Logger.addNamespace = function(namespace, logger) {
  loggers[namespace] = logger;
  logLevels[namespace] = ++maxLevel;
  logColors[namespace] = colors[lastIndex++];
  if (firstNamespace == '') {
    firstNamespace = namespace;
  }

  if (lastIndex > colors.length) {
    lastIndex = 0;
  }
};

/**
 * Expose `Application`
 */

exports = module.exports = Logger;
<commit_msg>used a global variable to improve support accross modules, also added support for SAND_LOG to set logs to view, off by default.<commit_after>/**
 * Module Dependencies
 */
var winston = require('winston');
var _ = require('lodash');

/**
 * Initialize a new `Application`.
 *
 * @api public
 */

function Logger(namespace) {
  "use strict";
  if (!(this instanceof Logger)) {
    return new Logger(namespace);
  }

  if (typeof namespace === 'undefined') {
    namespace = 'app';
  }

  if (typeof global.SandLog.loggers[namespace] !== 'undefined') {
    return global.SandLog.loggers[namespace];
  }

  this.namespace = namespace;

  Logger.addNamespace(namespace, this);

  setUpLogger();

  this.log = this.log.bind(this);
}

Logger.prototype.log = function() {
  var args = Array.prototype.slice.call(arguments);
  args.unshift(this.namespace);

  if (shouldShowLog(this.namespace)) {
    global.SandLog.logger.log.apply(global.SandLog.logger, args);
  }
};

function shouldShowLog(namespace) {
  var split = (process.env.SAND_LOG || '').split(/[\s,]+/);
  var len = split.length;

  var namespaces;
  for (var i = 0; i < len; i++) {
    if (!split[i]) continue; // ignore empty strings
    namespaces = split[i].replace(/\*/g, '.*?');
    if (namespaces[0] !== '-') {
      return (new RegExp('^' + namespaces + '$')).test(namespace);
    }
  }

  return false
}

/**
 * Private Variables
 */
if (_.isUndefined(global.SandLog)) {
  global.SandLog = {
    loggers: {},
    logger: null,
    levels: {},
    colors: {},
    transports: [
      new (winston.transports.Console)({
        colorize: true,
        prettyPrint: true
      })
    ],
    lastIndex: 0,
    firstNamespace: '',
    maxLevel: -1,
    level: 0
  };
}

var colors = [
  'cyan',
  'green',
  'blue',
  'magenta',
  'yellow',
  'red'
];

function setUpLogger() {
  global.SandLog.logger = new (winston.Logger)({
    transports: global.SandLog.transports,
    levels: global.SandLog.levels,
    colors: global.SandLog.colors
  });

  global.SandLog.level = global.SandLog.firstNamespace;

  _.each(global.SandLog.transports, function(transport) {
    transport.level = global.SandLog.firstNamespace;
  });
}

Logger.addTransport = function(transport) {
  global.SandLog.transports.push(transport);
  setUpLogger();
};

Logger.addNamespace = function(namespace, logger) {
  global.SandLog.loggers[namespace] = logger;
  global.SandLog.levels[namespace] = ++global.SandLog.maxLevel;
  global.SandLog.colors[namespace] = colors[global.SandLog.lastIndex++ % colors.length];
  if (global.SandLog.firstNamespace == '') {
    global.SandLog.firstNamespace = namespace;
  }
};

/**
 * Expose `Application`
 */

exports = module.exports = Logger;

{'fraction_non_alphanumeric': 0.16009689495705792, 'fraction_numerical': 0.0017617264919621229, 'mean_word_length': 3.532934131736527, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '3977581', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1470, 'n_words': 313}