-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Custom log levels #94
Comments
You need to set both var myCustomLevels = {
levels: {
foo: 0,
bar: 1,
baz: 2,
foobar: 3
},
colors: {
foo: 'blue',
bar: 'green',
baz: 'yellow',
foobar: 'red'
}
};
var customLevelLogger = new (winston.Logger)({
level: 'foo',
levels: myCustomLevels.levels,
transports: [new winston.transports.Console()]
});
customLevelLogger.foobar('some foobar level-ed message'); |
Hey indexzero, I'm running into the same issue working with a slightly simplified version of your example code: var winston = require('winston')
var myCustomLevels = {
levels: {
foo: 0,
bar: 1,
baz: 2,
foobar: 3
}
};
var customLevelLogger = new (winston.Logger)({
levels: myCustomLevels.levels,
transports: [new winston.transports.Console()]
});
customLevelLogger.foobar('some foobar level-ed message'); will not work. It prints nothing to the console:
If I modify the first two levels to include info and debug, I will get the expected result: var myCustomLevels = {
levels: {
info: 0,
debug: 1,
baz: 2,
foobar: 3
}
};
var customLevelLogger = new (winston.Logger)({
levels: myCustomLevels.levels,
transports: [new winston.transports.Console()]
});
customLevelLogger.foobar('some foobar level-ed message'); Will result in:
|
var customLevelLogger = new (winston.Logger)({
level: 'foo', // You are not passing a level parameter, so it is defaulting to `info` which doesnt exist.
levels: myCustomLevels.levels,
transports: [new winston.transports.Console()]
});
customLevelLogger.foobar('some foobar level-ed message'); |
Your exact code sample copy and pasted into a node script doesn't output anything to the terminal. Also, since you're specifying which level to use specifically ("foobar") in this case, why would it care about the default level? |
I've also tried both of the code examples you provided. Neither of them output anything to console. It's not until you include "debug" and "info" in the custom levels, that things will work as expected. Here is a video of me using your example code, and changing those 2 keys. http://screencast.com/t/CxWAleAueeRW Also, taking out |
So I was able to dig into this a bit more and figure out a bit more about what's going on. From all the code samples I've seen in the documentation and this thread, the expectation is that setting the default level on the logger is the only required configuration to use custom levels. I found that not only do you need to set the default logger level but also the default transport level to get the examples above and in the documentation to work as expected. From your example above, changing this line to the following:
This is mostly to do with this conditional in lib/winston/logger.js: if ((transport.level && self.levels[transport.level] <= self.levels[level])
|| (!transport.level && self.levels[self.level] <= self.levels[level])) Since the default transport.level is set to
You'll need to do one of two things to use custom levels, in addition to defining the default level on the logger:
If this isn't by design, it can be fixed by altering the conditional and the forced variable assignment. If it is by design, the documentation should be tweaked a bit too avoid confusion. I'd be happy to help accomplish either, just let me know which direction you'd like to head. |
Hey Index, Thanks! |
Thanks JCBarry, though it believe it should be ({level: "foobar"}). |
Seems that configuring winston is a bit of a black art, unless you're the author of course. If it's of any use to someone, here's my fully working example with 2 loggers, custom levels and console coloring: https://gist.github.com/2570757 |
That seems to work:
|
+1 @garth |
thanks @garth |
Hi, I had the same problem. I solved it by starting with "info : 0"
|
Strange, what I encountered: My levels were (I copied from the default system settings, and added the trace one) (require('winston').config.syslog.levels) {"emerg":0,"alert":1,"crit":2,"error":3,"warning":4,"notice":5,"info":6,"debug":7, "trace":8} When I inverted the order, it worked ... I found it strange since the syslog.levels are inverted. {"emerg":8,"alert":7,"crit":6,"error":5,"warning":4,"notice":3,"info":2,"debug":1, "trace":0} But its working properly now. |
Weird, but I confirm that it also worked for me after having the order inverted in comparison to syslog. |
+1 @lucastschmidt although I feel weird doing it. I'd like to know the reason why inverting the levels worked; was it something to do with conflicting values vs the syslog? |
Well @kb19, I have no idea actually, I didnt investigate the code. |
a more complete working set : https://github.com/Offirmo/html_tests/blob/master/incubator/_nodexps/winston_004_custom_levels/server.js |
Hi, I had the same problem. I solved it by change orders var config = {
}; |
+1 Doesn't work. Cannot get the colors to work at all. Tried all examples listed here and in the main docs. |
Yeah, I'm having the same problem. Tried everything, any hints about why it might not be working? |
+1 |
Ditto 👍 |
hi, |
check out my version: https://gist.github.com/vikas5914/cf568748ac89446e19ecd5e2e6900443 |
From comments on @vikas5914 s thread, I realized that the |
will someone update the docs with a working example? |
If anybody is wondering how to keep the default levels and add more:
|
I'm unable to get custom log levels to work. I've tried updating node, installing winston via npm, and cloning the winston repo. The example code in the readme for custom levels runs without any errors:
but nothing prints out into console.
I've also tried a variation of
with logger.addLevels() but I get the same results. The new log levels I pass do nothing.
The originals (info, debug, etc.) are not available - just as expected.
The text was updated successfully, but these errors were encountered: