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