温斯顿:了解日志记录级别 [英] Winston : understanding logging levels

查看:156
本文介绍了温斯顿:了解日志记录级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读和摆弄Winston时,我感到困惑的是,为什么记录级别按原样排序,为什么传输按照它们的行为方式运行(嗯,至少是控制台一个).如果有人可以甚至可以通过真实的用例示例来说明问题,我将不胜感激,为什么解释使用Winston进行日志记录的工作原理如此?

Reading and fiddling with Winston, I'm puzzled as to why the logging levels are ordered as they are and why the transports behave in the way they do (well, at least the Console one). I'd appreciate if someone could, perhaps even thoroughly, with real use case examples, explain why logging with Winston works this way?

例如,我这样设置记录器:

For example, I setup my logger like this :

var logger = new (winston.Logger)({
  levels: winston.config.syslog.levels,
  colors: winston.config.syslog.colors,
  level: "debug",  // I'm not sure what this option even does here???
  transports: [
    new (winston.transports.Console)({
      colorize: true,
      handleExceptions: true,
      json: false,
      level: "debug"
    })
  ]
});

因此,如果我执行logger.debug("Test");,则它将记录debug: Test,很好.但是,如果我执行logger.info("Test");,则什么也不会发生.

So, if I do logger.debug("Test");, then it will log debug: Test, fine. But if I do logger.info("Test");, then nothing happens.

我的问题是,如果我想登录控制台一切 但是 debug消息,我该怎么办? ...甚至debug info消息,但还要记录其他所有内容?

The problem I have is that, If I want to log to the console eveverything but debug messages, what do I do? ... or even debug and info messages, but log everything else?

来自Java世界,使用标准的记录器,我习惯使debugwarn更细粒度",并且记录器向后工作.例如,将日志记录级别设置为info确实记录了除debug(或其他内容)以外的所有内容.

Coming from a Java world, using the standard loggers, I am used to having debug being more "fine grained" than warn and the loggers worked backwards; setting the logging level to info, for example, did log everything but debug (or something).

此外,如果我希望记录器仅记录errorwarninginfo消息,我该如何使用Winston做到这一点?

Also, what if I'd like a logger to log only error, warning and info messages, how would I do that with Winston?

显然,此级别的顺序对于winston.config.syslog.levels是唯一的.因此剩下的唯一问题是:是否可以通过某种方式将传输限制为仅特定的日志记录级别?"

Apparently, this order of level is unique to winston.config.syslog.levels. So the only question remaining is : "Is it possible to, somehow, restrict a transport to a very specific logging level only?"

推荐答案

按照文档,您可以设置自己的日志记录级别,最低为0,并为其关联颜色.现在,如果您不想记录最低级别,只需将level属性设置为相应级别.默认情况下,控制台记录器的级别设置为info

As per the documentation, you can set your own Logging levels, 0 being lowest, and associate colours with it. Now, if you don't want to log the lowest level, just set the level property to the corresponding level. By default, the console logger has it's level set to info

所以,这是一个示例:

logger = new (winston.Logger)({
  levels: {
    'info': 0,
    'ok': 1,
    'error': 2
  }
  transports: [
    new (winston.transports.ConsoleTransport)(silent: options.silent, level: 'ok')
  ]
});

这篇关于温斯顿:了解日志记录级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆