<commit_before>var Logger = require('./logger');
var LoggerSettings = require('./settings');


var SEP = '.';
var ROOT = 'root';

function parentNames(name) {
  var names = [];
  var parts = name.split(SEP);
  while(parts.length) {
    names.push(parts.join(SEP));
    parts.pop();
  }
  names.push(ROOT);
  return names;
}

/**
 * Handler logger and its settings manipulation
 *
 * @class
 */
function LoggerFactory() {
  this._loggers = {};
  this._settings = {};
}

LoggerFactory.prototype = {

  /**
   * Returns logger with given name
   * @param  {string} name Name of logger
   * @return {Logger}
   */
  get: function(name) {
    var logger = this._loggers[name];
    if(!logger) {
      logger = this._createNewLogger(name);
      this._loggers[name] = logger;
    }
    return logger;
  },

  _createNewLogger: function(name) {
    var that = this;
    var chain = parentNames(name);
    var chainLength = chain.length;
    return new Logger(function(record) {
      for(var i = 0; i < chainLength; i++) {
        var settings = that._settings[chain[i]];
        if(settings) {
          settings.handle(record);
        }
      }
    }, name);
  },

  /**
   * Returns settings for logger with given name
   * @param  {string} name logger name
   * @return {LoggerSettings}
   */
  settings: function(name) {
    this._settings[name] = this._settings[name] || new LoggerSettings();
    return this._settings[name];
  }
};

module.exports = LoggerFactory;<commit_msg>Add simple settings to logger factory to make it uber fast for some cases<commit_after>var Logger = require('./logger');
var LoggerSettings = require('./settings');


var SEP = '.';
var ROOT = 'root';
var NOOP = function() {};

function parentNames(name) {
  var names = [];
  var parts = name.split(SEP);
  while(parts.length) {
    names.push(parts.join(SEP));
    parts.pop();
  }
  names.push(ROOT);
  return names;
}

/**
 * Handler logger and its settings manipulation
 *
 * @class
 */
function LoggerFactory(opts) {
  opts = opts || {};

  this._loggers = {};
  this._settings = {};

  this._useHierarchy = typeof opts.useHierarchy === 'undefined' ? true : !!opts.useHierarchy;
  this._useFixedLoggers = typeof opts.useFixedLoggers === 'undefined' ? false : !!opts.useFixedLoggers;
}

LoggerFactory.prototype = {
  /**
   * Set internal value to create loggers with parents. It means if it set to false
   * when logger created never assumed it has parent loggers.
   *
   * @param  {Boolean} value
   * @return {this}
   */
  setUseHierarchy: function(value) {
    this._useHierarchy = !!value;
    return this;
  },

  /**
   * Set internal value to fix loggers after creation. It literally means, that after logger
   * created its settings never changed.
   *
   * @param  {Boolean} value
   * @return {this}
   */
  setUseFixedLoggers: function(value) {
    this._useFixedLoggers = !!value;
    return this;
  },

  /**
   * Returns logger with given name
   * @param  {string} name Name of logger
   * @return {Logger}
   */
  get: function(name) {
    var logger = this._loggers[name];
    if(!logger) {
      logger = this._createNewLogger(name);
      this._loggers[name] = logger;
    }
    return logger;
  },

  _getProperOnLogCallback: function(name) {
    var that = this;
    var settings;
    var chain;
    var chainLength;

    if(this._useHierarchy) {
      if(this._useFixedLoggers) {

        chain = parentNames(name);
        chainLength = chain.length;
        settings = [];

        for(var i = 0; i < chainLength; i++) {
          var s = this._settings[chain[i]];
          if(s) {
            settings.push(s);
          }
        }

        var len = settings.length;
        return function onLog(record) {
          for(var i = 0; i < len; i++) {
            settings[i].handle(record);
          }
        };
      } else {
        chain = parentNames(name);
        chainLength = chain.length;

        return function onLog(record) {
          for(var i = 0; i < chainLength; i++) {
            var settings = that._settings[chain[i]];
            if(settings) {
              settings.handle(record);
            }
          }
        };
      }
    } else {
      if(this._useFixedLoggers) {
        settings = this._settings[name];

        return settings ?
          function onLog(record) {
            settings.handle(record);
          } :
          NOOP;
      } else {
        return function onLog(record) {
          var settings = that._settings[name];
          if(settings) {
            settings.handle(record);
          }
        };
      }
    }
  },

  _createNewLogger: function(name) {
    return new Logger(this._getProperOnLogCallback(name), name);
  },

  _createNewSettings: function() {
    return new LoggerSettings();
  },

  /**
   * Returns settings for logger with given name
   * @param  {string} name logger name
   * @return {LoggerSettings}
   */
  settings: function(name) {
    this._settings[name] = this._settings[name] || this._createNewSettings();
    return this._settings[name];
  }
};

module.exports = LoggerFactory;
{'fraction_non_alphanumeric': 0.13245424129108443, 'fraction_numerical': 0.000787246604999016, 'mean_word_length': 2.4199192462987886, 'pattern_counts': {'":': 0, '<': 7, '<?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': '7673423', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1533, 'n_words': 380}