question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom log levels

See original GitHub issue

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:

 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:closed
  • Created 12 years ago
  • Comments:28 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
JCBarrycommented, Feb 24, 2012

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:

transports: [new winston.transports.Console({level: foobar})]

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 info in lib/winston/transports/transport.js here:

this.level = options.level  || 'info';

You’ll need to do one of two things to use custom levels, in addition to defining the default level on the logger:

  1. Define a default level on your transport
  2. Include an info level in your custom levels

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. Thanks!

2reactions
lucasbagatinicommented, May 20, 2021

If anybody is wondering how to keep the default levels and add more:

const myCustomLevels = {
  levels: {
    ...winston.config.npm.levels,
    myCustomLevel: 7,
  },
  colors: {
    ...winston.config.npm.colors,
    myCustomLevel: 'blue',
  },
};
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found