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

Add stderrLevels option to Console Transport and update docs. #630

Closed
wants to merge 1 commit into from

Conversation

paulhroth
Copy link
Contributor

This PR lets users choose which levels to log to stderr instead of stdout. It's non-breaking and fully backwards compatible with previous options and behavior. I hope you like it 👍

The new Console Transport option is stderrLevels, which accepts an Array of strings containing the levels to log to stderr instead of stdout.

The code is thoroughly unit tested, contains input validation, and converts the Array provided to options.stderrLevels to an Object for faster key-lookup times. The README.md and docs/transports.md files have also been updated to better reflect the current state of the Console Transport.

Example usage:
Create a Console that logs 'error' and 'warn' levels to stderr, and other levels to stdout:
var console = new (winston.transports.Console)({ stderrLevels: ['error', 'warn'] });

Create a Console that logs nothing to stderr and everything to stdout:
var console = new (winston.transports.Console)({ stderrLevels: [] });

Create a Console that logs certain custom levels to stderr:

var console = new (winston.transports.Console)({ 
  levels: myCustomLevels,
  stderrLevels: ['delta', 'epsilon']
});

Backwards Compatibility
The current state of the Console has it logging these levels to stderr:

  • error and debug (if options.debugStdout is false)
  • error (if options.debugStdout is true)

This behavior is kept as the default for stderrLevels in order to ensure this change is non-breaking: stderrLevels defaults to the equivalent of (options.debugStdout ? ['error'] : ['error', 'debug']). This means that everyone not using the new stderrLevels option will see no change in the Console Transport's behavior.

Note: Another PR from July 2013 exists for the same feature (#286), but caused breaking changes and was never merged. Hopefully since this new PR is non-breaking and takes the debugStdout option into consideration, uses an Object for fast key-lookup times, includes unit tests, input validation and updated documentation, the feature we both want to see can finally get in in 2015 :)

@DominiqueLeroux
Copy link

Nice improvement! I also have been wanting to control level-to-output stream mapping for a while.

@ThibodeauJF
Copy link

Let's use that improvement the world needs it 👍

@yoannmoinet
Copy link

This seems to be a valuable improvement.

this.showLevel = options.showLevel === undefined ? true : options.showLevel;
this.label = options.label || null;
this.logstash = options.logstash || false;
this.stderrLevels = (function (levels, debugStdout) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An immediately invoked function expression is a rather cumbersome way to do this. Could you clean it up a bit?

@indexzero
Copy link
Member

I am +1 on this if we can cleanup the obtuse syntax a bit. There has to be a simpler way to setup the initial state.

@indexzero indexzero added the Feature Request Request for new functionality to support use cases not already covered label May 19, 2015
@paulhroth
Copy link
Contributor Author

@indexzero Thanks for the code review, I made some changes following your advice. How does this look? Should I move the setStderrLevels function out of the Console constructor (right now it's there so as to follow the helper-function pattern seen with throwIf in the other transports, e.g. in daily-rotate-file.js line 31)?

@paulhroth paulhroth mentioned this pull request May 26, 2015
- Let users choose which levels to log to stderr instead of stdout
- Maintain backwards compatibility with previous options/behavior
@indexzero
Copy link
Member

This is good. Especially the deprecation notice. This will land in 1.1 and we will fully remove debugStdout in 2.0.0

@indexzero indexzero closed this Oct 9, 2015
@indexzero
Copy link
Member

This was cherry-picked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request Request for new functionality to support use cases not already covered
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants