# All files / lib logger.js

79.55% Statements 35/44
25% Branches 3/12
61.54% Functions 8/13
81.4% Lines 35/43

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133

    2x
    2x

    2x

    2x

    3x
    3x
    3x
    3x
    3x

    1x

    1x

    1x

    3x
    3x
    3x

    3x

    3x

    3x
    3x

    2x
    2x

    2x

    14x

    14x
    14x
    14x

    14x

    14x

    2x

    4x

    2x
    2x
    2x
    2x
    2x
     /* eslint no-underscore-dangle:0 */

    'use strict';

    const levels = require('./levels');
    const EventEmitter = require('events');

    const DEFAULT_CATEGORY = '[default]';

    let logWritesEnabled = true;

    /**
     * @name LoggingEvent
     * @namespace Log4js
     */
    class LoggingEvent {
      /**
       * Models a logging event.
       * @constructor
       * @param {String} categoryName name of category
       * @param {Log4js.Level} level level of message
       * @param {Array} data objects to log
       * @param {Logger} logger the associated logger
       * @author Seth Chisamore
       */
      constructor(categoryName, level, data, logger) {
        this.startTime = new Date();
        this.categoryName = categoryName;
        this.data = data;
        this.level = level;
        this.logger = logger;
      }
    }

    /**
     * Logger to log messages.
     * use {@see log4js#getLogger(String)} to get an instance.
     *
     * @name Logger
     * @namespace Log4js
     * @param name name of category to log to
     * @param level
     *
     * @author Stephan Strittmatter
     */
    class Logger extends EventEmitter {
      constructor(name, level) {
        super();

        this.category = name || DEFAULT_CATEGORY;

        Iif (level) {
          this.setLevel(level);
        }
      }

      setLevel(level) {
        this.level = levels.toLevel(level, this.level || levels.TRACE);
      }

      removeLevel() {
        delete this.level;
      }

      log() {
        /* eslint prefer-rest-params:0 */
        // todo: once node v4 support dropped, use rest parameter instead
        const args = Array.from(arguments);
        const logLevel = levels.toLevel(args[0], levels.INFO);
        Iif (!this.isLevelEnabled(logLevel)) {
          return;
        }
        this._log(logLevel, args.slice(1));
      }

      isLevelEnabled(otherLevel) {
        return this.level.isLessThanOrEqualTo(otherLevel);
      }

      _log(level, data) {
        const loggingEvent = new LoggingEvent(this.category, level, data, this);
        this.emit('log', loggingEvent);
      }
    }

    Logger.DEFAULT_CATEGORY = DEFAULT_CATEGORY;
    Logger.prototype.level = levels.TRACE;

    ['Trace', 'Debug', 'Info', 'Warn', 'Error', 'Fatal', 'Mark'].forEach(addLevelMethods);

    function addLevelMethods(target) {
      const level = levels.toLevel(target);

      const levelStrLower = level.toString().toLowerCase();
      const levelMethod = levelStrLower.replace(/_([a-z])/g, g => g[1].toUpperCase());
      const isLevelMethod = levelMethod[0].toUpperCase() + levelMethod.slice(1);

      Logger.prototype[`is${isLevelMethod}Enabled`] = function () {
        return this.isLevelEnabled(level.toString());
      };

      Logger.prototype[levelMethod] = function () {
        /* eslint prefer-rest-params:0 */
        // todo: once node v4 support dropped, use rest parameter instead
        const args = Array.from(arguments);
        if (logWritesEnabled && this.isLevelEnabled(level)) {
          this._log(level, args);
        }
      };
    }

    /**
     * Disable all log writes.
     * @returns {void}
     */
    function disableAllLogWrites() {
      logWritesEnabled = false;
    }

    /**
     * Enable log writes.
     * @returns {void}
     */
    function enableAllLogWrites() {
      logWritesEnabled = true;
    }

    module.exports.LoggingEvent = LoggingEvent;
    module.exports.Logger = Logger;
    module.exports.disableAllLogWrites = disableAllLogWrites;
    module.exports.enableAllLogWrites = enableAllLogWrites;
    module.exports.addLevelMethods = addLevelMethods;