Code coverage report for lib/winston/transports/console.js

# All files / lib/winston/transports console.js

78.13% Statements 25/32
71.43% Branches 15/21
100% Functions 5/5
77.42% Lines 24/31

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
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101

    2x
    2x
    2x

    2x

    24x

    24x
    24x
    22x

    15x

    15x

    15x

    15x

    15x
    6x

    24x
    19x

    5x

    5x
    1x

    4x
    6x
    1x

    5x

    5x

     /* eslint-disable no-console */
    /*
     * console.js: Transport for outputting to the console.
     *
     * (C) 2010 Charlie Robbins
     * MIT LICENCE
     */

    'use strict';

    const os = require('os');
    const { LEVEL, MESSAGE } = require('triple-beam');
    const TransportStream = require('winston-transport');

    /**
     * Transport for outputting to the console.
     * @type {Console}
     * @extends {TransportStream}
     */
    module.exports = class Console extends TransportStream {
      /**
       * Constructor function for the Console transport object responsible for
       * persisting log messages and metadata to a terminal or TTY.
       * @param {!Object} [options={}] - Options for this instance.
       */
      constructor(options = {}) {
        super(options);

        // Expose the name of this Transport on the prototype
        this.name = 'console';
        this.stderrLevels = this._stringArrayToSet(options.stderrLevels);
        this.eol = options.eol || os.EOL;
      }

      /**
       * Core logging method exposed to Winston.
       * @param {Object} info - TODO: add param description.
       * @param {Function} callback - TODO: add param description.
       * @returns {undefined}
       */
      log(info, callback) {
        setImmediate(() => this.emit('logged', info));

        // Remark: what if there is no raw...?
        Iif (this.stderrLevels[info[LEVEL]]) {
          if (console._stderr) {
            // Node.js maps `process.stderr` to `console._stderr`.
            console._stderr.write(`${info[MESSAGE]}${this.eol}`);
          } else {
            // console.error adds a newline
            console.error(info[MESSAGE]);
          }

          if (callback) {
            callback(); // eslint-disable-line callback-return
          }
          return;
        }

        Eif (console._stdout) {
          // Node.js maps `process.stdout` to `console._stdout`.
          console._stdout.write(`${info[MESSAGE]}${this.eol}`);
        } else {
          // console.log adds a newline.
          console.log(info[MESSAGE]);
        }

        if (callback) {
          callback(); // eslint-disable-line callback-return
        }
      }

      /**
       * Returns a Set-like object with strArray's elements as keys (each with the
       * value true).
       * @param {Array} strArray - Array of Set-elements as strings.
       * @param {?string} [errMsg] - Custom error message thrown on invalid input.
       * @returns {Object} - TODO: add return description.
       * @private
       */
      _stringArrayToSet(strArray, errMsg) {
        if (!strArray)
          return {};

        errMsg = errMsg || 'Cannot make set from type other than Array of string elements';

        if (!Array.isArray(strArray)) {
          throw new Error(errMsg);
        }

        return strArray.reduce((set, el) =>  {
          if (typeof el !== 'string') {
            throw new Error(errMsg);
          }
          set[el] = true;

          return set;
        }, {});
      }
    };