Skip to content

Commit

Permalink
console transport: send all levels to stdout not stderr by default (w…
Browse files Browse the repository at this point in the history
…instonjs#1332)

* console transport: send 'debug' level output to stdout not stderr by default

* fix style

* Address PR comments / everything goes to stdout by default

* update doc
  • Loading branch information
DABH authored and indexzero committed Jun 1, 2018
1 parent 2800528 commit 1be7ec5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ coverage/*
.nyc_output/*
*.log
.idea
*.sw*
5 changes: 4 additions & 1 deletion UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@

### Transports
- `winston.transports.Memory` was removed. Use any Node.js `stream.Writeable` with a large `highWaterMark` instance instead.
- When writing transports use `winston-transport` instead of `winston.Transport`
- When writing transports use `winston-transport` instead of `winston.Transport`.
- In `winston.transports.Console`, output for all log levels is now sent to stdout by default.
- `debugStdout` option has been removed.
- `stderrLevels` now defaults to `[]`.

### `winston.Container` and `winston.loggers`
- `winston.Container` instances no longer have default `Console` transports
Expand Down
45 changes: 9 additions & 36 deletions lib/winston/transports/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ module.exports = class Console extends TransportStream {

// Expose the name of this Transport on the prototype
this.name = 'console';
this.stderrLevels = this._getStderrLevels(
options.stderrLevels,
options.debugStdout
);
this.stderrLevels = this._stringArrayToSet(options.stderrLevels);
this.eol = options.eol || os.EOL;
}

Expand Down Expand Up @@ -73,37 +70,6 @@ module.exports = class Console extends TransportStream {
}
}

/**
* Convert stderrLevels into an Object for faster key-lookup times than an
* Array. For backwards compatibility, stderrLevels defaults to
* ['error', 'debug'] or ['error'] depending on whether options.debugStdout
* is true.
* @param {mixed} levels - TODO: add param description.
* @param {mixed} debugStdout - TODO: add param description.
* @returns {mixed} - TODO: add return description.
* @private
*/
_getStderrLevels(levels, debugStdout) {
const defaultMsg = 'Cannot have non-string elements in stderrLevels Array';
if (debugStdout) {
if (levels) {
// Don't allow setting both debugStdout and stderrLevels together,
// since this could cause behaviour a programmer might not expect.
throw new Error('Cannot set debugStdout and stderrLevels together');
}

return this._stringArrayToSet(['error'], defaultMsg);
}

if (!levels) {
return this._stringArrayToSet(['error', 'debug'], defaultMsg);
} else if (!(Array.isArray(levels))) {
throw new Error('Cannot set stderrLevels to type other than Array');
}

return this._stringArrayToSet(levels, defaultMsg);
}

/**
* Returns a Set-like object with strArray's elements as keys (each with the
* value true).
Expand All @@ -113,7 +79,14 @@ module.exports = class Console extends TransportStream {
* @private
*/
_stringArrayToSet(strArray, errMsg) {
errMsg = errMsg || 'Cannot make set from Array with non-string elements';
if (!strArray)
return {};

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) => {
if (typeof el !== 'string') {
Expand Down
30 changes: 9 additions & 21 deletions test/transports/console.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const defaultLevels = winston.config.npm.levels;
const transports = {
defaults: new winston.transports.Console(),
noStderr: new winston.transports.Console({ stderrLevels: [] }),
debugStdout: new winston.transports.Console({ debugStdout: true }),
stderrLevels: new winston.transports.Console({
stderrLevels: ['info', 'warn']
}),
Expand Down Expand Up @@ -56,7 +55,7 @@ function assertStderrLevels(transport, stderrLevels) {

describe('Console transport', function () {
describe('with defaults', function () {
it('logs all levels (EXCEPT error and debug) to stdout', function () {
it('logs all levels to stdout', function () {
stdMocks.use();
transports.defaults.levels = defaultLevels;
Object.keys(defaultLevels)
Expand All @@ -74,43 +73,32 @@ describe('Console transport', function () {
stdMocks.restore();
var output = stdMocks.flush();
assume(output.stderr).is.an('array');
assume(output.stderr).length(2);
assume(output.stderr).length(0);
assume(output.stdout).is.an('array');
assume(output.stdout).length(5);
assume(output.stdout).length(7);
});

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

describe('throws an appropriate error when', function () {
it('if both debugStdout and stderrLevels are set { debugStdout, stderrLevels }', function () {
assume(function () {
let throwing = new winston.transports.Console({
stderrLevels: ['foo', 'bar'],
debugStdout: true
})
}).throws(/Cannot set debugStdout and stderrLevels/);
});

it("if stderrLevels is set, but not an Array { stderrLevels: 'Not an Array' }", function () {
assume(function () {
let throwing = new winston.transports.Console({
stderrLevels: 'Not an Array',
debugStdout: false
stderrLevels: 'Not an Array'
})
}).throws(/Cannot set stderrLevels to type other than Array/);
}).throws(/Cannot make set from type other than Array of string elements/);
});

it("if stderrLevels contains non-string elements { stderrLevels: ['good', /^invalid$/, 'valid']", function () {
assume(function () {
let throwing = new winston.transports.Console({
stderrLevels: ['good', /^invalid$/, 'valid'],
debugStdout: false
stderrLevels: ['good', /^invalid$/, 'valid']
})
}).throws(/Cannot have non-string elements in stderrLevels Array/);
}).throws(/Cannot make set from type other than Array of string elements/);
});
});

Expand Down

0 comments on commit 1be7ec5

Please sign in to comment.