index.js 6.6 KB

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