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

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

95.65% Statements 22/23
92.31% Branches 12/13
100% Functions 3/3
95.45% Lines 21/22

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

    1x
    1x
    1x
    1x

    1x

    20x

    20x
    1x

    19x
    19x
    19x
    19x

    19x
    19x
    18x
    18x
    17x

    18x

    1x
    1x

    1x

     /**
     * stream.js: Transport for outputting to any arbitrary stream.
     *
     * (C) 2010 Charlie Robbins
     * MIT LICENCE
     */

    'use strict';

    const isStream = require('is-stream');
    const { MESSAGE } = require('triple-beam');
    const os = require('os');
    const TransportStream = require('winston-transport');

    /**
     * Transport for outputting to any arbitrary stream.
     * @type {Stream}
     * @extends {TransportStream}
     */
    module.exports = class Stream 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);

        if (!options.stream || !isStream(options.stream)) {
          throw new Error('options.stream is required.');
        }

        // We need to listen for drain events when write() returns false. This can
        // make node mad at times.
        this._stream = options.stream;
        this._stream.setMaxListeners(Infinity);
        this.isObjectMode = options.stream._writableState.objectMode;
        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));
        if (this.isObjectMode) {
          this._stream.write(info);
          if (callback) {
            callback(); // eslint-disable-line callback-return
          }
          return;
        }

        this._stream.write(`${info[MESSAGE]}${this.eol}`);
        Iif (callback) {
          callback(); // eslint-disable-line callback-return
        }
        return;
      }
    };