index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. 'use strict';
  2. var util = require('util');
  3. var Writable = require('readable-stream/lib/_stream_writable.js');
  4. var _require = require('triple-beam'),
  5. LEVEL = _require.LEVEL;
  6. /**
  7. * Constructor function for the TransportStream. This is the base prototype
  8. * that all `winston >= 3` transports should inherit from.
  9. * @param {Object} options - Options for this TransportStream instance
  10. * @param {String} options.level - Highest level according to RFC5424.
  11. * @param {Boolean} options.handleExceptions - If true, info with
  12. * { exception: true } will be written.
  13. * @param {Function} options.log - Custom log function for simple Transport
  14. * creation
  15. * @param {Function} options.close - Called on "unpipe" from parent.
  16. */
  17. var TransportStream = module.exports = function TransportStream() {
  18. var _this = this;
  19. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  20. Writable.call(this, { objectMode: true, highWaterMark: options.highWaterMark });
  21. this.format = options.format;
  22. this.level = options.level;
  23. this.handleExceptions = options.handleExceptions;
  24. this.handleRejections = options.handleRejections;
  25. this.silent = options.silent;
  26. if (options.log) this.log = options.log;
  27. if (options.logv) this.logv = options.logv;
  28. if (options.close) this.close = options.close;
  29. // Get the levels from the source we are piped from.
  30. this.once('pipe', function (logger) {
  31. // Remark (indexzero): this bookkeeping can only support multiple
  32. // Logger parents with the same `levels`. This comes into play in
  33. // the `winston.Container` code in which `container.add` takes
  34. // a fully realized set of options with pre-constructed TransportStreams.
  35. _this.levels = logger.levels;
  36. _this.parent = logger;
  37. });
  38. // If and/or when the transport is removed from this instance
  39. this.once('unpipe', function (src) {
  40. // Remark (indexzero): this bookkeeping can only support multiple
  41. // Logger parents with the same `levels`. This comes into play in
  42. // the `winston.Container` code in which `container.add` takes
  43. // a fully realized set of options with pre-constructed TransportStreams.
  44. if (src === _this.parent) {
  45. _this.parent = null;
  46. if (_this.close) {
  47. _this.close();
  48. }
  49. }
  50. });
  51. };
  52. /*
  53. * Inherit from Writeable using Node.js built-ins
  54. */
  55. util.inherits(TransportStream, Writable);
  56. /**
  57. * Writes the info object to our transport instance.
  58. * @param {mixed} info - TODO: add param description.
  59. * @param {mixed} enc - TODO: add param description.
  60. * @param {function} callback - TODO: add param description.
  61. * @returns {undefined}
  62. * @private
  63. */
  64. TransportStream.prototype._write = function _write(info, enc, callback) {
  65. if (this.silent || info.exception === true && !this.handleExceptions) {
  66. return callback(null);
  67. }
  68. // Remark: This has to be handled in the base transport now because we
  69. // cannot conditionally write to our pipe targets as stream. We always
  70. // prefer any explicit level set on the Transport itself falling back to
  71. // any level set on the parent.
  72. var level = this.level || this.parent && this.parent.level;
  73. if (!level || this.levels[level] >= this.levels[info[LEVEL]]) {
  74. if (info && !this.format) {
  75. return this.log(info, callback);
  76. }
  77. var errState = void 0;
  78. var transformed = void 0;
  79. // We trap(and re-throw) any errors generated by the user-provided format, but also
  80. // guarantee that the streams callback is invoked so that we can continue flowing.
  81. try {
  82. transformed = this.format.transform(Object.assign({}, info), this.format.options);
  83. } catch (err) {
  84. errState = err;
  85. }
  86. if (errState || !transformed) {
  87. // eslint-disable-next-line callback-return
  88. callback();
  89. if (errState) throw errState;
  90. return;
  91. }
  92. return this.log(transformed, callback);
  93. }
  94. this._writableState.sync = false;
  95. return callback(null);
  96. };
  97. /**
  98. * Writes the batch of info objects (i.e. "object chunks") to our transport
  99. * instance after performing any necessary filtering.
  100. * @param {mixed} chunks - TODO: add params description.
  101. * @param {function} callback - TODO: add params description.
  102. * @returns {mixed} - TODO: add returns description.
  103. * @private
  104. */
  105. TransportStream.prototype._writev = function _writev(chunks, callback) {
  106. if (this.logv) {
  107. var infos = chunks.filter(this._accept, this);
  108. if (!infos.length) {
  109. return callback(null);
  110. }
  111. // Remark (indexzero): from a performance perspective if Transport
  112. // implementers do choose to implement logv should we make it their
  113. // responsibility to invoke their format?
  114. return this.logv(infos, callback);
  115. }
  116. for (var i = 0; i < chunks.length; i++) {
  117. if (!this._accept(chunks[i])) continue;
  118. if (chunks[i].chunk && !this.format) {
  119. this.log(chunks[i].chunk, chunks[i].callback);
  120. continue;
  121. }
  122. var errState = void 0;
  123. var transformed = void 0;
  124. // We trap(and re-throw) any errors generated by the user-provided format, but also
  125. // guarantee that the streams callback is invoked so that we can continue flowing.
  126. try {
  127. transformed = this.format.transform(Object.assign({}, chunks[i].chunk), this.format.options);
  128. } catch (err) {
  129. errState = err;
  130. }
  131. if (errState || !transformed) {
  132. // eslint-disable-next-line callback-return
  133. chunks[i].callback();
  134. if (errState) {
  135. // eslint-disable-next-line callback-return
  136. callback(null);
  137. throw errState;
  138. }
  139. } else {
  140. this.log(transformed, chunks[i].callback);
  141. }
  142. }
  143. return callback(null);
  144. };
  145. /**
  146. * Predicate function that returns true if the specfied `info` on the
  147. * WriteReq, `write`, should be passed down into the derived
  148. * TransportStream's I/O via `.log(info, callback)`.
  149. * @param {WriteReq} write - winston@3 Node.js WriteReq for the `info` object
  150. * representing the log message.
  151. * @returns {Boolean} - Value indicating if the `write` should be accepted &
  152. * logged.
  153. */
  154. TransportStream.prototype._accept = function _accept(write) {
  155. var info = write.chunk;
  156. if (this.silent) {
  157. return false;
  158. }
  159. // We always prefer any explicit level set on the Transport itself
  160. // falling back to any level set on the parent.
  161. var level = this.level || this.parent && this.parent.level;
  162. // Immediately check the average case: log level filtering.
  163. if (info.exception === true || !level || this.levels[level] >= this.levels[info[LEVEL]]) {
  164. // Ensure the info object is valid based on `{ exception }`:
  165. // 1. { handleExceptions: true }: all `info` objects are valid
  166. // 2. { exception: false }: accepted by all transports.
  167. if (this.handleExceptions || info.exception !== true) {
  168. return true;
  169. }
  170. }
  171. return false;
  172. };
  173. /**
  174. * _nop is short for "No operation"
  175. * @returns {Boolean} Intentionally false.
  176. */
  177. TransportStream.prototype._nop = function _nop() {
  178. // eslint-disable-next-line no-undefined
  179. return void undefined;
  180. };
  181. // Expose legacy stream
  182. module.exports.LegacyTransportStream = require('./legacy');