最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Winston add custom log levels - Stack Overflow

programmeradmin4浏览0评论

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

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

Share Improve this question asked Jun 12, 2018 at 8:55 PatrickPatrick 3493 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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

发布评论

评论列表(0)

  1. 暂无评论