# all files / winston-transport/ index.js

68% Statements 17/25
55.56% Branches 10/18
50% Functions 2/4
68% Lines 17/25

    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

    1×

    1×
    5×
    5×
    5×
    5×
    5×
    5×

    5×

    5×

    5×

    1×

    1×
    5×
    1×

    4×
    4×

     'use strict';

    var stream = require('stream'),
        util = require('util');

    var TransportStream = module.exports = function TransportStream(opts) {
      stream.Writable.call(this, { objectMode: true });
      opts = opts || {};
      this.format = opts.format;
      this.level = opts.level;
      this.handleExceptions = opts.handleExceptions;
      this.log = this.log || opts.log;

      var self = this;

      //
      // Get the levels from the source we are piped from.
      //
      this.once('pipe', function (logger) {
        self.levels = logger.levels;
        self.level = self.level || logger.level;
        //
        // TODO: Improve bookkeeping here to support pipe and unpipe
        // from multiple LogStream parents (e.g. Containers)
        //
        self.parent = logger;
      });

      //
      // If and/or when the transport is removed from this instance
      //
      this.once('unpipe', function (src) {
        //
        // TODO: Improve bookkeeping here to support pipe and unpipe
        // from multiple LogStream parents (e.g. Containers)
        //
        if (src === self.parent) {
          //
          // Remark: this may not be the desired implementation since
          // a single transport may be shared by multiple Logger
          // instances.
          //
          // Remark: depending on the use-case we may need to clean up
          // this.levels and this.level.
          //
          this.parent = null;
          if (self.close) {
            self.close();
          }
        }
      });
    };

    util.inherits(TransportStream, stream.Writable);

    /*
     * @private function _write(info)
     * Writes the info object to our transport instance.
     */
    TransportStream.prototype._write = function (info, enc, callback) {
      if (info.exception && !this.handleExceptions) {
        return callback(null);
      }

      //
      // Remark: This has to be handled in the base transport now because we cannot
      // conditionally write to our pipe targets as stream.
      //
      Eif (!this.level || this.levels[this.level] >= this.levels[info.level]) {
        return this.log(info, callback);
      }

      return callback(null);
    };