简体   繁体   中英

Winston add custom log levels

I try to add custom log levels to winston. This is the code for the logger:

const write = new (winston.Logger)({
    transports: [       
        new (winston.transports.DailyRotateFile)({
            filename: `${logDir}/%DATE%-log`,
            timestamp: tsFormat,
            datePattern: 'D-M-YYYY',
            prepend: true,
            zippedArchive:true,
         }),                
    ] 
});

I have tried to add custom log levels, but I continue to see all logs in my log file.

This is my code for the custom log levels:

var levels = {
levels: {
  info: 0,
  debug: 1,
  warning: 2,
  error: 3
}
};

And then I added this line of code for transport:

levels: levels.levels

And this in my transport:

 level: "error"

But I also keep seeing logs of info. Anyone can help me out with this? Thanks

Logging levels in winston are based on the priority (higher to lower). Severity of the logs are numerically ascending from most important to least.

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

Here when you do logging for error level 3 (logger.error) the logs under crit, alert and emerg will also included in your logs.

Likewise in your custom log level, severity for level:error is very low as well it logs all the levels <= 3 including log, debug and warning.

If you want to log only level:error modify your custom log level severity as following

var levels = {
levels: {
  error: 0
  info: 1,
  debug: 2,
  warning: 3
}
};

For more information checkout the winston logging levels

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM