Winston : understanding logging levels

梦想的初衷 提交于 2019-12-21 07:14:05

问题


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"
    })
  ]
});

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

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?

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).

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

* EDIT *

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?"


回答1:


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

So, here is an example:

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



回答2:


var logger = new (winston.Logger)({
       levels: {
        'info': 0,
        'ok': 1,
        'error': 2
       },
    colors: {
        'info': 'red',
        'ok': 'green',
        'error': 'yellow'
       },
    transports: [
        new (winston.transports.Console)({level:'info',colorize: true})
    ]
});
logger.log('info',"This is info level");
logger.info("This is info level");


来源:https://stackoverflow.com/questions/20931089/winston-understanding-logging-levels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!