Code coverage report for lib/winston/create-logger.js

# All files / lib/winston create-logger.js

90.48% Statements 19/21
88.89% Branches 8/9
100% Functions 5/5
90.48% Lines 19/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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

    7x
    7x
    7x
    7x

    75x
    69x

    69x
    480x
    480x

    480x

    14831x
    14829x
    14829x
    14829x
    14829x
    14828x

    2x

    7x
    75x

     /**
     * create-logger.js: Logger factory for winston logger instances.
     *
     * (C) 2010 Charlie Robbins
     * MIT LICENCE
     */

    'use strict';

    const { LEVEL } = require('triple-beam');
    const config = require('./config');
    const Logger = require('./logger');
    const debug = require('diagnostics')('winston:create-logger');

    /**
     * DerivedLogger to attach the logs level methods.
     * @type {DerivedLogger}
     * @extends {Logger}
     */
    class DerivedLogger extends Logger {
      /**
       * Create a new class derived logger for which the levels can be attached to
       * the prototype of. This is a V8 optimization that is well know to increase
       * performance of prototype functions.
       * @param {!Object} options - Options for the created logger.
       */
      constructor(options) {
        super(options);
        this._setupLevels();
      }

      /**
       * Create the log level methods for the derived logger.
       * @returns {undefined}
       * @private
       */
      _setupLevels() {
        Object.keys(this.levels).forEach(level => {
          debug('Define prototype method for "%s"', level);
          Iif (level === 'log') {
            // eslint-disable-next-line no-console
            console.warn('Level "log" not defined: conflicts with the method "log". Use a different level name.');
            return;
          }

          // Define prototype methods for each log level
          // e.g. logger.log('info', msg) <––> logger.info(msg)
          this[level] = (...args) => {
            // Optimize the hot-path which is the single object.
            if (args.length === 1) {
              const [msg] = args;
              const info = msg && msg.message && msg || { message: msg };
              info.level = info[LEVEL] = level;
              this.write(info);
              return this;
            }

            // Otherwise build argument list which could potentially conform to
            // either:
            // . v3 API: log(obj)
            // 2. v1/v2 API: log(level, msg, ... [string interpolate], [{metadata}], [callback])
            return this.log(level, ...args);
          };
        });
      }
    }

    /**
     * Create a new instance of a winston Logger. Creates a new
     * prototype for each instance.
     * @param {!Object} opts - Options for the created logger.
     * @returns {Logger} - A newly created logger instance.
     */
    module.exports = (opts = { levels: config.npm.levels }) => (
      new DerivedLogger(opts)
    );