legacy.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict';
  2. const util = require('util');
  3. const { LEVEL } = require('triple-beam');
  4. const TransportStream = require('./');
  5. /**
  6. * Constructor function for the LegacyTransportStream. This is an internal
  7. * wrapper `winston >= 3` uses to wrap older transports implementing
  8. * log(level, message, meta).
  9. * @param {Object} options - Options for this TransportStream instance.
  10. * @param {Transpot} options.transport - winston@2 or older Transport to wrap.
  11. */
  12. const LegacyTransportStream = module.exports = function LegacyTransportStream(options = {}) {
  13. TransportStream.call(this, options);
  14. if (!options.transport || typeof options.transport.log !== 'function') {
  15. throw new Error('Invalid transport, must be an object with a log method.');
  16. }
  17. this.transport = options.transport;
  18. this.level = this.level || options.transport.level;
  19. this.handleExceptions = this.handleExceptions || options.transport.handleExceptions;
  20. // Display our deprecation notice.
  21. this._deprecated();
  22. // Properly bubble up errors from the transport to the
  23. // LegacyTransportStream instance, but only once no matter how many times
  24. // this transport is shared.
  25. function transportError(err) {
  26. this.emit('error', err, this.transport);
  27. }
  28. if (!this.transport.__winstonError) {
  29. this.transport.__winstonError = transportError.bind(this);
  30. this.transport.on('error', this.transport.__winstonError);
  31. }
  32. };
  33. /*
  34. * Inherit from TransportStream using Node.js built-ins
  35. */
  36. util.inherits(LegacyTransportStream, TransportStream);
  37. /**
  38. * Writes the info object to our transport instance.
  39. * @param {mixed} info - TODO: add param description.
  40. * @param {mixed} enc - TODO: add param description.
  41. * @param {function} callback - TODO: add param description.
  42. * @returns {undefined}
  43. * @private
  44. */
  45. LegacyTransportStream.prototype._write = function _write(info, enc, callback) {
  46. if (this.silent || (info.exception === true && !this.handleExceptions)) {
  47. return callback(null);
  48. }
  49. // Remark: This has to be handled in the base transport now because we
  50. // cannot conditionally write to our pipe targets as stream.
  51. if (!this.level || this.levels[this.level] >= this.levels[info[LEVEL]]) {
  52. this.transport.log(info[LEVEL], info.message, info, this._nop);
  53. }
  54. callback(null);
  55. };
  56. /**
  57. * Writes the batch of info objects (i.e. "object chunks") to our transport
  58. * instance after performing any necessary filtering.
  59. * @param {mixed} chunks - TODO: add params description.
  60. * @param {function} callback - TODO: add params description.
  61. * @returns {mixed} - TODO: add returns description.
  62. * @private
  63. */
  64. LegacyTransportStream.prototype._writev = function _writev(chunks, callback) {
  65. for (let i = 0; i < chunks.length; i++) {
  66. if (this._accept(chunks[i])) {
  67. this.transport.log(
  68. chunks[i].chunk[LEVEL],
  69. chunks[i].chunk.message,
  70. chunks[i].chunk,
  71. this._nop
  72. );
  73. chunks[i].callback();
  74. }
  75. }
  76. return callback(null);
  77. };
  78. /**
  79. * Displays a deprecation notice. Defined as a function so it can be
  80. * overriden in tests.
  81. * @returns {undefined}
  82. */
  83. LegacyTransportStream.prototype._deprecated = function _deprecated() {
  84. // eslint-disable-next-line no-console
  85. console.error([
  86. `${this.transport.name} is a legacy winston transport. Consider upgrading: `,
  87. '- Upgrade docs: https://github.com/winstonjs/winston/blob/master/UPGRADE-3.0.md'
  88. ].join('\n'));
  89. };
  90. /**
  91. * Clean up error handling state on the legacy transport associated
  92. * with this instance.
  93. * @returns {undefined}
  94. */
  95. LegacyTransportStream.prototype.close = function close() {
  96. if (this.transport.close) {
  97. this.transport.close();
  98. }
  99. if (this.transport.__winstonError) {
  100. this.transport.removeListener('error', this.transport.__winstonError);
  101. this.transport.__winstonError = null;
  102. }
  103. };