legacy.js 3.8 KB

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