# All files / lib log4js.js

55.96% Statements 108/193
52.69% Branches 49/93
53.49% Functions 23/43
56.25% Lines 108/192

    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
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541

    2x
    2x
    2x
    2x
    2x
    2x

    2x

    2x
    2x
    2x
    2x
    2x

    2x

    3x

    2x

    6x

    3x
    3x
    3x

    3x

    3x

    1x

    1x

    1x
    1x
    1x
    1x
    1x

    1x

    3x

    6x
    6x

    6x
    3x

    6x
    3x

    6x

    6x
    6x

    6x
    3x

    3x
    2x
    2x

    3x

    6x
    4x

    6x

    4x
    4x
    4x

    3x
    3x
    3x
    3x

    3x
    3x
    3x
    3x

    3x
    3x

    2x

    2x

    2x

    3x
    3x
    3x
    3x
    3x

    3x

    3x
    3x
    3x

    3x
    2x

    2x

    1x

    3x

    2x

    3x
    15x

    4x
    4x

    4x

    4x

    4x

    4x
    4x
    1x

    4x

    2x

    2x

    2x

    2x

    2x
    2x
    2x
    2x

    2x
    2x
    2x

    2x

    2x
    2x

    2x

    2x

    2x

    2x
     /* eslint no-prototype-builtins:1,no-restricted-syntax:[1, "ForInStatement"],no-plusplus:0 */

    'use strict';

    /**
     * @fileoverview log4js is a library to log in JavaScript in similar manner
     * than in log4j for Java. The API should be nearly the same.
     *
     * <h3>Example:</h3>
     * <pre>
     *  let logging = require('log4js');
     *  //add an appender that logs all messages to stdout.
     *  logging.addAppender(logging.consoleAppender());
     *  //add an appender that logs 'some-category' to a file
     *  logging.addAppender(logging.fileAppender('file.log'), 'some-category');
     *  //get a logger
     *  let log = logging.getLogger('some-category');
     *  log.setLevel(logging.levels.TRACE); //set the Level
     *
     *  ...
     *
     *  //call the log
     *  log.trace('trace me' );
     * </pre>
     *
     * NOTE: the authors below are the original browser-based log4js authors
     * don't try to contact them about bugs in this version :)
     * @version 1.0
     * @author Stephan Strittmatter - http://jroller.com/page/stritti
     * @author Seth Chisamore - http://www.chisamore.com
     * @since 2005-05-20
     * @static
     * Website: http://log4js.berlios.de
     */
    const fs = require('fs');
    const util = require('util');
    const layouts = require('./layouts');
    const levels = require('./levels');
    const loggerModule = require('./logger');
    const connectLogger = require('./connect-logger').connectLogger;

    const Logger = loggerModule.Logger;

    const ALL_CATEGORIES = '[all]';
    const loggers = {};
    const appenderMakers = {};
    const appenderShutdowns = {};
    const defaultConfig = {
      appenders: [
        { type: 'stdout' }
      ],
      replaceConsole: false
    };

    let appenders = {};

    function hasLogger(logger) {
      return loggers.hasOwnProperty(logger);
    }

    // todo: this method should be moved back to levels.js, but for loop require, need some refactor
    levels.forName = function (levelStr, levelVal) {
      let level;
      if (typeof levelStr === 'string' && typeof levelVal === 'number') {
        const levelUpper = levelStr.toUpperCase();
        level = new levels.Level(levelVal, levelUpper);
        loggerModule.addLevelMethods(level);
      }
      return level;
    };

    function getBufferedLogger(categoryName) {
      const baseLogger = getLogger(categoryName);
      const logger = {};
      logger.temp = [];
      logger.target = baseLogger;
      logger.flush = function () {
        for (let i = 0; i < logger.temp.length; i++) {
          const log = logger.temp[i];
          logger.target[log.level](log.message);
          delete logger.temp[i];
        }
      };
      logger.trace = function (message) {
        logger.temp.push({ level: 'trace', message: message });
      };
      logger.debug = function (message) {
        logger.temp.push({ level: 'debug', message: message });
      };
      logger.info = function (message) {
        logger.temp.push({ level: 'info', message: message });
      };
      logger.warn = function (message) {
        logger.temp.push({ level: 'warn', message: message });
      };
      logger.error = function (message) {
        logger.temp.push({ level: 'error', message: message });
      };
      logger.fatal = function (message) {
        logger.temp.push({ level: 'fatal', message: message });
      };

      return logger;
    }

    function normalizeCategory(category) {
      return `${category}.`;
    }

    function doesLevelEntryContainsLogger(levelCategory, loggerCategory) {
      const normalizedLevelCategory = normalizeCategory(levelCategory);
      const normalizedLoggerCategory = normalizeCategory(loggerCategory);
      return normalizedLoggerCategory.substring(0, normalizedLevelCategory.length) === normalizedLevelCategory;
    }

    function doesAppenderContainsLogger(appenderCategory, loggerCategory) {
      const normalizedAppenderCategory = normalizeCategory(appenderCategory);
      const normalizedLoggerCategory = normalizeCategory(loggerCategory);
      return normalizedLoggerCategory.substring(0, normalizedAppenderCategory.length) === normalizedAppenderCategory;
    }

    /**
     * Get a logger instance. Instance is cached on categoryName level.
     * @static
     * @param loggerCategoryName
     * @return {Logger} instance of logger for the category
     */
    function getLogger(loggerCategoryName) {
      // Use default logger if categoryName is not specified or invalid
      Iif (typeof loggerCategoryName !== 'string') {
        loggerCategoryName = Logger.DEFAULT_CATEGORY;
      }

      if (!hasLogger(loggerCategoryName)) {
        let level;

        /* jshint -W073 */
        // If there's a 'levels' entry in the configuration
        Iif (levels.config) {
          // Goes through the categories in the levels configuration entry,
          // starting with the 'higher' ones.
          const keys = Object.keys(levels.config).sort();
          for (let idx = 0; idx < keys.length; idx++) {
            const levelCategory = keys[idx];
            if (doesLevelEntryContainsLogger(levelCategory, loggerCategoryName)) {
              // level for the logger
              level = levels.config[levelCategory];
            }
          }
        }
        /* jshint +W073 */

        // Create the logger for this name if it doesn't already exist
        loggers[loggerCategoryName] = new Logger(loggerCategoryName, level);

        /* jshint -W083 */
        let appenderList;
        for (const appenderCategory in appenders) {
          Eif (doesAppenderContainsLogger(appenderCategory, loggerCategoryName)) {
            appenderList = appenders[appenderCategory];
            appenderList.forEach((appender) => {
              loggers[loggerCategoryName].addListener('log', appender);
            });
          }
        }
        /* jshint +W083 */

        Iif (appenders[ALL_CATEGORIES]) {
          appenderList = appenders[ALL_CATEGORIES];
          appenderList.forEach((appender) => {
            loggers[loggerCategoryName].addListener('log', appender);
          });
        }
      }

      return loggers[loggerCategoryName];
    }

    /**
     * args are appender, optional shutdown function, then zero or more categories
     */
    function addAppender() {
      /* eslint prefer-rest-params:0 */
      // todo: once node v4 support dropped, use rest parameter instead
      let args = Array.from(arguments);
      const appender = args.shift();
      // check for a shutdown fn
      if (args.length > 0 && typeof args[0] === 'function') {
        appenderShutdowns[appender] = args.shift();
      }

      if (args.length === 0 || args[0] === undefined) {
        args = [ALL_CATEGORIES];
      }
      // argument may already be an array
      Iif (Array.isArray(args[0])) {
        args = args[0];
      }

      args.forEach((appenderCategory) => {
        addAppenderToCategory(appender, appenderCategory);

        if (appenderCategory === ALL_CATEGORIES) {
          addAppenderToAllLoggers(appender);
        } else {
          for (const loggerCategory in loggers) {
            Eif (doesAppenderContainsLogger(appenderCategory, loggerCategory)) {
              loggers[loggerCategory].addListener('log', appender);
            }
          }
        }
      });
    }

    function addAppenderToAllLoggers(appender) {
      for (const logger in loggers) {
        if (hasLogger(logger)) {
          loggers[logger].addListener('log', appender);
        }
      }
    }

    function addAppenderToCategory(appender, category) {
      if (!appenders[category]) {
        appenders[category] = [];
      }
      appenders[category].push(appender);
    }

    function clearAppenders() {
      // if we're calling clearAppenders, we're probably getting ready to write
      // so turn log writes back on, just in case this is after a shutdown
      loggerModule.enableAllLogWrites();
      appenders = {};
      for (const logger in loggers) {
        if (hasLogger(logger)) {
          loggers[logger].removeAllListeners('log');
        }
      }
    }

    function configureAppenders(appenderList, options) {
      clearAppenders();
      Eif (appenderList) {
        appenderList.forEach((appenderConfig) => {
          loadAppender(appenderConfig.type);
          let appender;
          appenderConfig.makers = appenderMakers;
          try {
            appender = appenderMakers[appenderConfig.type](appenderConfig, options);
            addAppender(appender, appenderConfig.category);
          } catch (e) {
            throw new Error(`log4js configuration problem for ${util.inspect(appenderConfig)}`, e);
          }
        });
      }
    }

    function configureLevels(_levels) {
      levels.config = _levels; // Keep it so we can create loggers later using this cfg
      Iif (_levels) {
        const keys = Object.keys(levels.config).sort();

        /* eslint-disable guard-for-in */
        for (const idx in keys) {
          const category = keys[idx];
          if (category === ALL_CATEGORIES) {
            setGlobalLogLevel(_levels[category]);
          }

          for (const loggerCategory in loggers) {
            if (doesLevelEntryContainsLogger(category, loggerCategory)) {
              loggers[loggerCategory].setLevel(_levels[category]);
            }
          }
        }
      }
    }

    function setGlobalLogLevel(level) {
      Logger.prototype.level = levels.toLevel(level, levels.TRACE);
    }

    /**
     * Get the default logger instance.
     * @return {Logger} instance of default logger
     * @static
     */
    function getDefaultLogger() {
      return getLogger(Logger.DEFAULT_CATEGORY);
    }

    const configState = {};

    function loadConfigurationFile(filename) {
      Iif (filename) {
        return JSON.parse(fs.readFileSync(filename, 'utf8'));
      }
      return undefined;
    }

    function configureOnceOff(config, options) {
      Eif (config) {
        try {
          restoreConsole();
          configureLevels(config.levels);
          configureAppenders(config.appenders, options);

          Iif (config.replaceConsole) {
            replaceConsole();
          }
        } catch (e) {
          throw new Error(
            `Problem reading log4js config ${util.inspect(config)}. Error was '${e.message}' (${e.stack})`
          );
        }
      }
    }

    function reloadConfiguration(options) {
      const mtime = getMTime(configState.filename);
      if (!mtime) return;

      if (configState.lastMTime && (mtime.getTime() > configState.lastMTime.getTime())) {
        configureOnceOff(loadConfigurationFile(configState.filename), options);
      }
      configState.lastMTime = mtime;
    }

    function getMTime(filename) {
      let mtime;
      try {
        mtime = fs.statSync(configState.filename).mtime;
      } catch (e) {
        getLogger('log4js').warn(`Failed to load configuration file ${filename}`);
      }
      return mtime;
    }

    function initReloadConfiguration(filename, options) {
      if (configState.timerId) {
        clearInterval(configState.timerId);
        delete configState.timerId;
      }
      configState.filename = filename;
      configState.lastMTime = getMTime(filename);
      configState.timerId = setInterval(reloadConfiguration, options.reloadSecs * 1000, options);
    }

    function configure(configurationFileOrObject, options) {
      let config = configurationFileOrObject;
      config = config || process.env.LOG4JS_CONFIG;
      options = options || {};

      if (config === undefined || config === null || typeof config === 'string') {
        Iif (options.reloadSecs) {
          initReloadConfiguration(config, options);
        }
        config = loadConfigurationFile(config) || defaultConfig;
      } else {
        Iif (options.reloadSecs) { // eslint-disable-line
          getLogger('log4js').warn(
            'Ignoring configuration reload parameter for "object" configuration.'
          );
        }
      }
      configureOnceOff(config, options);
    }

    const originalConsoleFunctions = {
      log: console.log,
      debug: console.debug,
      info: console.info,
      warn: console.warn,
      error: console.error
    };

    function replaceConsole(logger) {
      function replaceWith(fn) {
        return function () {
          /* eslint prefer-rest-params:0 */
          // todo: once node v4 support dropped, use rest parameter instead
          fn.apply(logger, Array.from(arguments));
        };
      }

      logger = logger || getLogger('console');

      ['log', 'debug', 'info', 'warn', 'error'].forEach((item) => {
        console[item] = replaceWith(item === 'log' ? logger.info : logger[item]);
      });
    }

    function restoreConsole() {
      ['log', 'debug', 'info', 'warn', 'error'].forEach((item) => {
        console[item] = originalConsoleFunctions[item];
      });
    }

    /* eslint global-require:0 */
    /**
     * Load an appenderModule based on the provided appender filepath. Will first
     * check if the appender path is a subpath of the log4js 'lib/appenders' directory.
     * If not, it will attempt to load the the appender as complete path.
     *
     * @param {string} appender The filepath for the appender.
     * @returns {Object|null} The required appender or null if appender could not be loaded.
     * @private
     */
    function requireAppender(appender) {
      let appenderModule;
      try {
        appenderModule = require(`./appenders/${appender}`); // eslint-disable-line
      } catch (e) {
        appenderModule = require(appender); // eslint-disable-line
      }
      return appenderModule;
    }

    /**
     * Load an appender. Provided the appender path to be loaded. If appenderModule is defined,
     * it will be used in place of requiring the appender module.
     *
     * @param {string} appender The path to the appender module.
     * @param {Object|void} [appenderModule] The pre-required appender module. When provided,
     * instead of requiring the appender by its path, this object will be used.
     * @returns {void}
     * @private
     */
    function loadAppender(appender, appenderModule) {
      appenderModule = appenderModule || requireAppender(appender);

      Iif (!appenderModule) {
        throw new Error(`Invalid log4js appender: ${util.inspect(appender)}`);
      }

      log4js.appenders[appender] = appenderModule.appender.bind(appenderModule);
      if (appenderModule.shutdown) {
        appenderShutdowns[appender] = appenderModule.shutdown.bind(appenderModule);
      }
      appenderMakers[appender] = appenderModule.configure.bind(appenderModule);
    }

    /**
     * Shutdown all log appenders. This will first disable all writing to appenders
     * and then call the shutdown function each appender.
     *
     * @params {Function} cb - The callback to be invoked once all appenders have
     *  shutdown. If an error occurs, the callback will be given the error object
     *  as the first argument.
     */
    function shutdown(cb) {
      // First, disable all writing to appenders. This prevents appenders from
      // not being able to be drained because of run-away log writes.
      loggerModule.disableAllLogWrites();

      // turn off config reloading
      Iif (configState.timerId) {
        clearInterval(configState.timerId);
      }

      // Call each of the shutdown functions in parallel
      let completed = 0;
      let error;
      const shutdownFunctions = [];

      function complete(err) {
        error = error || err;
        completed++;
        Eif (completed >= shutdownFunctions.length) {
          cb(error);
        }
      }

      for (const category in appenderShutdowns) {
        Eif (appenderShutdowns.hasOwnProperty(category)) {
          shutdownFunctions.push(appenderShutdowns[category]);
        }
      }

      Iif (!shutdownFunctions.length) {
        return cb();
      }

      shutdownFunctions.forEach((shutdownFct) => {
        shutdownFct(complete);
      });

      return null;
    }

    /**
     * @name log4js
     * @namespace Log4js
     * @property getBufferedLogger
     * @property getLogger
     * @property getDefaultLogger
     * @property hasLogger
     * @property addAppender
     * @property loadAppender
     * @property clearAppenders
     * @property configure
     * @property shutdown
     * @property replaceConsole
     * @property restoreConsole
     * @property levels
     * @property setGlobalLogLevel
     * @property layouts
     * @property appenders
     * @property appenderMakers
     * @property connectLogger
     */
    const log4js = {
      getBufferedLogger,
      getLogger,
      getDefaultLogger,
      hasLogger,

      addAppender,
      loadAppender,
      clearAppenders,
      configure,
      shutdown,

      replaceConsole,
      restoreConsole,

      levels,
      setGlobalLogLevel,

      layouts,
      appenders: {},
      appenderMakers,
      connectLogger
    };

    module.exports = log4js;

    // set ourselves up
    configure();