Skip to content

Commit

Permalink
add cconsoleWarnLevels property to options to support console.warn us…
Browse files Browse the repository at this point in the history
…age (#1373)

* add cconsoleWarnLevels property to optionos to support console.warn usage

* add cconsoleWarnLevels property to optionos to support console.warn usage

* remove yarn.lock

* CR fixes
  • Loading branch information
revik authored and DABH committed Jul 22, 2018
1 parent 1293221 commit cf424be
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ coverage/*
*.log
.idea
*.sw*
yarn.lock
1 change: 1 addition & 0 deletions docs/transports.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ The Console transport takes a few simple options:
* __silent:__ Boolean flag indicating whether to suppress output (default false).
* __eol:__ string indicating the end-of-line characters to use (default `os.EOL`)
* __stderrLevels__ Array of strings containing the levels to log to stderr instead of stdout, for example `['error', 'debug', 'info']`. (default `[]`)
* __consoleWarnLevels__ Array of strings containing the levels to log using console.warn or to stderr (in Node.js) instead of stdout, for example `['warn', 'debug']`. (default `[]`)

### File Transport
``` js
Expand Down
15 changes: 15 additions & 0 deletions lib/winston/transports/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = class Console extends TransportStream {
// Expose the name of this Transport on the prototype
this.name = 'console';
this.stderrLevels = this._stringArrayToSet(options.stderrLevels);
this.consoleWarnLevels = this._stringArrayToSet(options.consoleWarnLevels);
this.eol = options.eol || os.EOL;
}

Expand All @@ -51,6 +52,20 @@ module.exports = class Console extends TransportStream {
console.error(info[MESSAGE]);
}

if (callback) {
callback(); // eslint-disable-line callback-return
}
return;
} else if (this.consoleWarnLevels[info[LEVEL]]) {
if (console._stderr) {
// Node.js maps `process.stderr` to `console._stderr`.
// in Node.js console.warn is an alias for console.error
console._stderr.write(`${info[MESSAGE]}${this.eol}`);
} else {
// console.warn adds a newline
console.warn(info[MESSAGE]);
}

if (callback) {
callback(); // eslint-disable-line callback-return
}
Expand Down
33 changes: 23 additions & 10 deletions test/transports/console.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ const transports = {
defaults: new winston.transports.Console(),
noStderr: new winston.transports.Console({ stderrLevels: [] }),
stderrLevels: new winston.transports.Console({
stderrLevels: ['info', 'warn']
stderrLevels: ['info', 'error']
}),
consoleWarnLevels: new winston.transports.Console({
consoleWarnLevels: ['warn', 'debug']
}),
eol: new winston.transports.Console({ eol: 'X' }),
syslog: new winston.transports.Console({
Expand All @@ -40,19 +43,22 @@ const transports = {

/**
* Returns a function that asserts the `transport` has the specified
* `stderrLevels`.
* logLevels values in the appropriate logLevelsName member.
*
* @param {TransportStream} transport Transport to assert against
* @param {Array} stderrLevels Set of levels assumed to exist
* @param {Array} logLevels Set of levels assumed to exist for the specified map
* @param {String} logLevelsName The name of the array/map that holdes the log leveles values (ie: 'stderrLevels', 'consoleWarnLevels')
* @return {function} Assertion function to execute comparison
*/
function assertStderrLevels(transport, stderrLevels) {
function assertLogLevelsValues(transport, logLevels, logLevelsName = 'stderrLevels') {
return function () {
assume(JSON.stringify(Object.keys(transport.stderrLevels).sort()))
.equals(JSON.stringify(stderrLevels.sort()));
assume(JSON.stringify(Object.keys(transport[logLevelsName]).sort()))
.equals(JSON.stringify(logLevels.sort()));
};
}



describe('Console transport', function () {
describe('with defaults', function () {
it('logs all levels to stdout', function () {
Expand All @@ -78,9 +84,10 @@ describe('Console transport', function () {
assume(output.stdout).length(7);
});

it("should set stderrLevels to [] by default", assertStderrLevels(
it("should set stderrLevels to [] by default", assertLogLevelsValues(
transports.defaults,
[]
[],
'stderrLevels'
));
});

Expand All @@ -102,9 +109,15 @@ describe('Console transport', function () {
});
});

it("{ stderrLevels: ['info', 'warn'] } logs to them appropriately", assertStderrLevels(
it("{ stderrLevels: ['info', 'error'] } logs to them appropriately", assertLogLevelsValues(
transports.stderrLevels,
['info', 'warn']
['info', 'error'],
'stderrLevels'
));
it("{ consoleWarnLevels: ['warn', 'debug'] } logs to them appropriately", assertLogLevelsValues(
transports.consoleWarnLevels,
['warn', 'debug'],
'consoleWarnLevels'
));

it('{ eol } adds a custom EOL delimiter', function (done) {
Expand Down

0 comments on commit cf424be

Please sign in to comment.