modern.js 6.7 KB

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