Custom log levels
See original GitHub issueI’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:
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)({ levels: myCustomLevels.levels });
customLevelLogger.foobar('some foobar level-ed message');
but nothing prints out into console.
I’ve also tried a variation of
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: 'somefile.log' })
]
});
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.
Issue Analytics
- State:
- Created 12 years ago
- Comments:28 (2 by maintainers)
Top Results From Across the Web
Log4j – Custom Log Levels - Apache Logging Services
Custom log levels can be defined in code or in configuration. To define a custom log level in code, use the Level.forName() method....
Read more >Log4J2 | Create custom log levels and how to use them
Create custom log level ... First of all, we need to have Log4J2 added as a dependency in the build.gradle file. We need...
Read more >mannysah/custom-log-levels - GitHub
FunnyLogLevel.java This class defines the custom log level with the name FUNNY. The log level of FUNNY is lesser than ERROR log level,...
Read more >Log4j Levels Example - Order, Priority, Custom Filters
Log4j Level Order/Priority Trace is of the lowest priority and Fatal is having highest priority. Below is the log4j logging level order. Trace ......
Read more >how to add custom logging level in log4j - java - Stack Overflow
Log4J 2 supports custom log levels. Custom log levels can be defined in code or in configuration. To create custom log levels in ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
Since the default transport.level is set to
info
in lib/winston/transports/transport.js here:You’ll need to do one of two things to use custom levels, in addition to defining the default level on the logger:
info
level in your custom levelsIf 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. Thanks!
If anybody is wondering how to keep the default levels and add more: