<commit_before>'use strict';

/*
TODO:
- browser support
- documentation
- express request and response routing (express-winston-2)
- http or ws logging (winstond, winston-tagged-http-logger?)
- loggly
- mail (winston-mail)
- file
-
 */

var winston = require('winston');
var util = require('util');
var _ = require('lodash');
var color = require('cli-color');
var moment = require('moment');
var EventEmitter = require('events').EventEmitter;
var loggerEventBus = new EventEmitter();
var prevColor = 0;

exports = module.exports = logger;
exports.debugLogger = debugLogger;

//If they want to use a transport not in the list, they need to use
//.transports.add after creating the logger
exports.availableTransports = {
  console: winston.transports.Console,
  file: winston.transports.File,
  webhook: winston.transports.Webhook,
  http: winston.transports.Http,
  daily: require('winston-daily-rotate-file')
};

exports.enable = enable;
exports.disable = disable;
exports.disableAll = disableAll;
exports.enabled = enabled;
exports.load = load;
exports.save = save;
exports.timestamp = timestamp;
exports.formatter = formatter;
exports.colors = [
  'green',
  'blue',
  'magenta',
  'cyan',
  'greenBright',
  'blueBright',
  'magentaBright',
  'cyanBright'
];

/**
 * The currently active debug mode names, and names to skip.
 */
exports.names = [];
exports.skips = [];

function logger(namespace, options){
  var actualLogger = function(){};

  loggerEventBus.on('disable', function(namespace){
    if (namespace === actualLogger.namespace && namespace !== 'APP') {
      actualLogger('disabled');
      actualLogger.enabled = false;
    }
  });

  loggerEventBus.on('enable', function(namespace){
    if (namespace === actualLogger.namespace) {
      actualLogger.enabled = true;
      actualLogger('enabled');
    }
  });

  if (exports.enabled(namespace) || namespace === 'APP'){
    actualLogger = getEnabledLogger(namespace, options);
    actualLogger.enabled = true;
  } else {
    actualLogger = disabled;
  }

  actualLogger.namespace = namespace;

  return actualLogger;
}

function getEnabledLogger(namespace, options){
  options.color = randomColor();
  var logger = createLogger(namespace, options);
  return createEnabledLogger(namespace, logger);
}

function randomColor() {
  return exports.colors[prevColor++ % exports.colors.length];
}

function createLogger(namespace, options){
  winston.loggers.add(namespace, getTransportConfiguration(namespace, options));
  return winston.loggers.get(namespace);
}

function getTransportConfiguration(namespace, options){
  var transports = _.create(options.transports);

  for (var transport in transports){
    var transportOptions = transports[transport];
    if (transport === 'console'){
      transportOptions = _.merge({
        timestamp: timestamp,
        formatter: formatter,
        colorize: true,<commit_msg>Add support for daily rotate file transport<commit_after>'use strict';

/*
 TODO:
 - browser support
 - documentation
 - express request and response routing (express-winston-2)
 - http or ws logging (winstond, winston-tagged-http-logger?)
 - loggly
 - mail (winston-mail)
 - file
 -
 */

var winston = require('winston');
var util = require('util');
var _ = require('lodash');
var color = require('cli-color');
var moment = require('moment');
var EventEmitter = require('events').EventEmitter;
var loggerEventBus = new EventEmitter();
var prevColor = 0;
var Daily = require('winston-daily-rotate-file');

exports = module.exports = logger;
exports.debugLogger = debugLogger;

// If they want to use a transport not in the list, they need to use
// .add after creating the logger
exports.availableTransports = {
  console: winston.transports.Console,
  file: winston.transports.File,
  webhook: winston.transports.Webhook,
  http: winston.transports.Http
};

exports.enable = enable;
exports.disable = disable;
exports.disableAll = disableAll;
exports.enabled = enabled;
exports.load = load;
exports.save = save;
exports.timestamp = timestamp;
exports.formatter = formatter;
exports.colors = [
  'green',
  'blue',
  'magenta',
  'cyan',
  'greenBright',
  'blueBright',
  'magentaBright',
  'cyanBright'
];

/**
 * The currently active debug mode names, and names to skip.
 */
exports.names = [];
exports.skips = [];

function logger(namespace, options){
  var actualLogger = function(){};

  loggerEventBus.on('disable', function(namespace){
    if (namespace === actualLogger.namespace && namespace !== 'APP') {
      actualLogger('disabled');
      actualLogger.enabled = false;
    }
  });

  loggerEventBus.on('enable', function(namespace){
    if (namespace === actualLogger.namespace) {
      actualLogger.enabled = true;
      actualLogger('enabled');
    }
  });

  if (exports.enabled(namespace) || namespace === 'APP'){
    actualLogger = getEnabledLogger(namespace, options);
    actualLogger.enabled = true;
  } else {
    actualLogger = disabled;
  }

  actualLogger.namespace = namespace;

  return actualLogger;
}

function getEnabledLogger(namespace, options){
  options.color = randomColor();
  var logger = createLogger(namespace, options);
  return createEnabledLogger(namespace, logger);
}

function randomColor() {
  return exports.colors[prevColor++ % exports.colors.length];
}

function createLogger(namespace, options){
  var transportConfig = getTransportConfiguration(namespace, options);
  if (transportConfig.dailyRotateFile) {
    winston.loggers.options.transports = [
      new Daily(transportConfig.dailyFileRotate)
    ]
    delete transportConfig.dailyFileRotate;
  }
  winston.loggers.add(namespace, transportConfig);
  var logger = winston.loggers.get(namespace);
  return logger;
}

function getTransportConfiguration(namespace, options){
  var transports = _.create({}, options.transports);
  for (var transport in transports){
    var transportOptions = transports[transport];
    if (transport === 'console'){
      transportOptions = _.merge({
        timestamp: timestamp,
        formatter: formatter,
        colorize: true,
{'fraction_non_alphanumeric': 0.12853130679002148, 'fraction_numerical': 0.0006608293408227325, 'mean_word_length': 4.371783496007098, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4952526', 'n_tokens_mistral': 1757, 'n_tokens_neox': 1723, 'n_words': 438}