Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save a reference to console methods in console transport #2498

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
* @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 @@
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 @@ -42,7 +49,7 @@
* @param {Function} callback - TODO: add param description.
* @returns {undefined}
*/
log(info, callback) {

Check warning on line 52 in lib/winston/transports/console.js

View workflow job for this annotation

GitHub Actions / Tests (16)

Method 'log' has too many statements (19). Maximum allowed is 15

Check warning on line 52 in lib/winston/transports/console.js

View workflow job for this annotation

GitHub Actions / Tests (16)

Method 'log' has a complexity of 12. Maximum allowed is 11

Check warning on line 52 in lib/winston/transports/console.js

View workflow job for this annotation

GitHub Actions / Tests (18)

Method 'log' has too many statements (19). Maximum allowed is 15

Check warning on line 52 in lib/winston/transports/console.js

View workflow job for this annotation

GitHub Actions / Tests (18)

Method 'log' has a complexity of 12. Maximum allowed is 11

Check warning on line 52 in lib/winston/transports/console.js

View workflow job for this annotation

GitHub Actions / Tests (20)

Method 'log' has too many statements (19). Maximum allowed is 15

Check warning on line 52 in lib/winston/transports/console.js

View workflow job for this annotation

GitHub Actions / Tests (20)

Method 'log' has a complexity of 12. Maximum allowed is 11
setImmediate(() => this.emit('logged', info));

// Remark: what if there is no raw...?
Expand All @@ -52,7 +59,7 @@
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 @@
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 @@
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 @@
* @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
Loading