Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent of log.IsDebugEnabled in Winston?

Is there an equivalent of log.IsDebugEnabled in Winston?

I want to use this to skip expensive logging code in a production environment but have it execute in development.

For example:

if(winston.isDebugEnabled){
   // Call to expensive dump routine here
   dump();
}

Checking winston.debug just checks whether the method is defined, not whether it is enabled.

Many thanks!


Edit: Added code example.

like image 795
Thomas Bratt Avatar asked Nov 01 '22 11:11

Thomas Bratt


1 Answers

I've added a method to my logger to achieve just that:

logger.isLevelEnabled = function(level) {
  return _.any(this.transports, function(transport) {
    return (transport.level && this.levels[transport.level] <= this.levels[level])
      || (!transport.level && this.levels[this.level] <= this.levels[level]);
  }, this);
};

This goes through each of your logger's transports and checks whether it 'wants' to log the specified level. Note _.any is lodash, you can replace with for loop.

like image 100
Omri Sivan Avatar answered Nov 17 '22 07:11

Omri Sivan