简体   繁体   English

温斯顿添加自定义日志级别

[英]Winston add custom log levels

I try to add custom log levels to winston. 我尝试将自定义日志级别添加到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. 在这里,当您为错误级别3(logger.error)进行日志记录时,crit,alert和emerg下的日志也将包含在日志中。

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. 同样,在您的自定义日志级别中, level:error严重性也很低,它会记录所有<= 3级别,包括日志,调试和警告。

If you want to log only level:error modify your custom log level severity as following 如果您只想记录level:error请按如下所示修改自定义日志级别的严重性

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

For more information checkout the winston logging levels 有关更多信息,请查看Winston日志记录级别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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