Skip to content

Commit

Permalink
Save a reference to console methods in console transport (#2498)
Browse files Browse the repository at this point in the history
* Save a reference to console methods
Fixes #2497

* Address PR feedback
  • Loading branch information
neilenns authored Aug 8, 2024
1 parent 4ff0538 commit e82752f
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/winston/transports/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ const TransportStream = require('winston-transport');
* @extends {TransportStream}
*/
module.exports = class Console extends TransportStream {
// Keep a reference to the log, warn, and error console methods
// in case they get redirected to this transport after the logger is
// instantiated. This prevents a circular reference issue.
_consoleLog = console.log.bind(console);
_consoleWarn = console.warn.bind(console);
_consoleError = console.error.bind(console);

/**
* Constructor function for the Console transport object responsible for
* persisting log messages and metadata to a terminal or TTY.
Expand All @@ -30,7 +37,7 @@ module.exports = class Console extends TransportStream {
this.name = options.name || 'console';
this.stderrLevels = this._stringArrayToSet(options.stderrLevels);
this.consoleWarnLevels = this._stringArrayToSet(options.consoleWarnLevels);
this.eol = (typeof options.eol === 'string') ? options.eol : os.EOL;
this.eol = typeof options.eol === 'string' ? options.eol : os.EOL;
this.forceConsole = options.forceConsole || false;

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

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

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

if (callback) {
Expand All @@ -97,16 +104,16 @@ module.exports = class Console extends TransportStream {
* @private
*/
_stringArrayToSet(strArray, errMsg) {
if (!strArray)
return {};
if (!strArray) return {};

errMsg = errMsg || 'Cannot make set from type other than Array of string elements';
errMsg =
errMsg || 'Cannot make set from type other than Array of string elements';

if (!Array.isArray(strArray)) {
throw new Error(errMsg);
}

return strArray.reduce((set, el) => {
return strArray.reduce((set, el) => {
if (typeof el !== 'string') {
throw new Error(errMsg);
}
Expand Down

0 comments on commit e82752f

Please sign in to comment.