Skip to content

Commit e82752f

Browse files
authored
Save a reference to console methods in console transport (#2498)
* Save a reference to console methods Fixes #2497 * Address PR feedback
1 parent 4ff0538 commit e82752f

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

lib/winston/transports/console.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ const TransportStream = require('winston-transport');
1818
* @extends {TransportStream}
1919
*/
2020
module.exports = class Console extends TransportStream {
21+
// Keep a reference to the log, warn, and error console methods
22+
// in case they get redirected to this transport after the logger is
23+
// instantiated. This prevents a circular reference issue.
24+
_consoleLog = console.log.bind(console);
25+
_consoleWarn = console.warn.bind(console);
26+
_consoleError = console.error.bind(console);
27+
2128
/**
2229
* Constructor function for the Console transport object responsible for
2330
* persisting log messages and metadata to a terminal or TTY.
@@ -30,7 +37,7 @@ module.exports = class Console extends TransportStream {
3037
this.name = options.name || 'console';
3138
this.stderrLevels = this._stringArrayToSet(options.stderrLevels);
3239
this.consoleWarnLevels = this._stringArrayToSet(options.consoleWarnLevels);
33-
this.eol = (typeof options.eol === 'string') ? options.eol : os.EOL;
40+
this.eol = typeof options.eol === 'string' ? options.eol : os.EOL;
3441
this.forceConsole = options.forceConsole || false;
3542

3643
this.setMaxListeners(30);
@@ -52,7 +59,7 @@ module.exports = class Console extends TransportStream {
5259
console._stderr.write(`${info[MESSAGE]}${this.eol}`);
5360
} else {
5461
// console.error adds a newline
55-
console.error(info[MESSAGE]);
62+
this._consoleError(info[MESSAGE]);
5663
}
5764

5865
if (callback) {
@@ -66,7 +73,7 @@ module.exports = class Console extends TransportStream {
6673
console._stderr.write(`${info[MESSAGE]}${this.eol}`);
6774
} else {
6875
// console.warn adds a newline
69-
console.warn(info[MESSAGE]);
76+
this._consoleWarn(info[MESSAGE]);
7077
}
7178

7279
if (callback) {
@@ -80,7 +87,7 @@ module.exports = class Console extends TransportStream {
8087
console._stdout.write(`${info[MESSAGE]}${this.eol}`);
8188
} else {
8289
// console.log adds a newline.
83-
console.log(info[MESSAGE]);
90+
this._consoleLog(info[MESSAGE]);
8491
}
8592

8693
if (callback) {
@@ -97,16 +104,16 @@ module.exports = class Console extends TransportStream {
97104
* @private
98105
*/
99106
_stringArrayToSet(strArray, errMsg) {
100-
if (!strArray)
101-
return {};
107+
if (!strArray) return {};
102108

103-
errMsg = errMsg || 'Cannot make set from type other than Array of string elements';
109+
errMsg =
110+
errMsg || 'Cannot make set from type other than Array of string elements';
104111

105112
if (!Array.isArray(strArray)) {
106113
throw new Error(errMsg);
107114
}
108115

109-
return strArray.reduce((set, el) => {
116+
return strArray.reduce((set, el) => {
110117
if (typeof el !== 'string') {
111118
throw new Error(errMsg);
112119
}

0 commit comments

Comments
 (0)