Skip to content
Merged
Changes from 1 commit
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 @@ 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;
_consoleWarn = console.warn;
_consoleError = console.error;

/**
* 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