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

"level" option to transports appears to have no effect #202

Closed
robbles opened this issue Nov 25, 2012 · 9 comments
Closed

"level" option to transports appears to have no effect #202

robbles opened this issue Nov 25, 2012 · 9 comments

Comments

@robbles
Copy link

robbles commented Nov 25, 2012

Maybe I've misunderstood this feature, but when I pass a "level" option to a transport, I expect it to restrict output to that level or higher (or perhaps to that level only).

var logger = new (winston.Logger)({
  transports: [
    new (winston.transports.Console)({level: 'warn'}),
  ]
});

logger.debug("this shouldn't show up, but it does");

Instead, it logs everything, include calls to logger.debug. Is there actually a way to restrict logging to a certain level? As an example use case, I want to see debug messages in development, but keep those out of production logs.

The docs seem to indicate that this should work the way I expect though:

Winston allows you to set a level on each transport that specifies the level of messages this transport should log. For example, you could log only errors to the console, with the full logs in a file"

@sbeam
Copy link

sbeam commented Dec 2, 2012

same issue here. But there is different behaviour for File and Console transports:

File logs everything no matter what level is set to.
set Console to 'debug', then I get only debug and error messages, but not info or warn.

@robbles
Copy link
Author

robbles commented Dec 3, 2012

I actually worked out what the problem is here - the default set of levels is just in a strange order, but you can configure them differently. It's not a bug in winston, just an odd default.

The default set is in winston.config.cli.levels and looks like:

{
    silly: 0,
    input: 1,
    verbose: 2,
    prompt: 3,
    info: 4,
    data: 5,
    help: 6,
    warn: 7,
    debug: 8,
    error: 9
}

Which makes very little sense to me since debug is higher than warn.

I changed my loggers to use winston.config.syslog.levels, which matches the more common error levels seen in other logging libraries:

{
    debug: 0,
    info: 1,
    notice: 2,
    warning: 3,
    error: 4,
    crit: 5,
    alert: 6,
    emerg: 7
}

You can pass a level argument to the Logger constructor, and possibly to individual transports:

logger = new (winston.Logger)({
  levels: (winston.config.syslog.levels),
  transports: logTransports
});

Now you can set the level to 'info' and see messages of level 'info' -> 'emerg' and not 'debug'. Hope that helps - this default behaviour should really be documented somewhere since it's different from any other logging library I've used.

@sbeam
Copy link

sbeam commented Dec 3, 2012

ok, makes sense. syslog levels are so ingrained I didn't even think to question it. thanks for documenting that, big help

@BryanDonovan
Copy link

This stumped me too. For future reference, this stems from npmlog, which uses these strange log levels: https://github.com/isaacs/npmlog

@ghost
Copy link

ghost commented Jan 24, 2013

+1 /stops banging head on keyboard now.

Plus what @garth shows in this gist. https://gist.github.com/2570757
levels need to be defined in both the Logger options which are passed into the constructor and in the Transport options.

This must be a bug.

@danmilon
Copy link

Bumped into this also. It feels so wrong and confusing! Should be documented at least.

@bfirsh
Copy link

bfirsh commented Feb 20, 2013

+1. Spent 15 mins trying to fix this today. This is bizarre.

@mansona
Copy link

mansona commented Mar 20, 2013

I think the default should be changed, it seems that a lot of people are being stumped by these odd defaults

@killroy42
Copy link
Contributor

It seems to me the http transport needs this line in the constructor, similar to the console transport:
Transport.call(this, options);

as well as this near the top:
var Transport = require('./transport').Transport;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants