35

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

Community
  • 1
  • 1
Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
  • 2
    indeed there was a bug in winston pre 2.0 : `winston.config.syslog.levels` had its levels' numeric values in reverse order than default values ! Using them makes winston unusable. This has been fixed in 2.0.0 : https://github.com/winstonjs/winston/blob/master/CHANGELOG.md#v200--2015-10-29 – Offirmo Feb 26 '16 at 15:34
  • Since this question has received a lot of views: in winston 3.0 it is possible to use a custom formatter to restrict transports to log only specific level[s]: https://stackoverflow.com/a/56918394/4451284 – davewy Jul 06 '19 at 23:46

2 Answers2

14

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')
  ]
});
kumarharsh
  • 18,961
  • 8
  • 72
  • 100
  • Yes, I am aware of reversing the order of the levels. However, this is not answering why the official levels are the way they are and all. ALso, it does not answer the question about having a transport (i.e. Console) intercepting only certain levels. In your example, the transport would echo any `ok` and `error`, and not just `ok`. – Yanick Rochon Jan 05 '14 at 08:18
  • Intuitively, I presume that you'd want to echo above a given level. I do not know if there is functionality in winston to filter only specific logs, but as a work-around, you can juggle your levels so that `OK` has higher priority than `ERROR`, that way you can filter it out... I'll look around to see if winston can filter the logs for a single level. – kumarharsh Jan 05 '14 at 08:35
  • Yes, I think that is what is happening. You can't set specific levels to be shown while ignoring higher levels. See [Issue #89](https://github.com/flatiron/winston/issues/89) – kumarharsh Jan 05 '14 at 08:40
  • 1
    Thanks, even though that issue was over 2 years ago, it made me realize that the root of the problem was the `syslog` levels/colors, as the default levels do not have this issue. I edited my question to reflect this. – Yanick Rochon Jan 05 '14 at 19:01
  • Ha Ha... Didn't think 2 years later I would reading this and would find your answer. –  Aug 03 '16 at 08:25
  • @pyros2097 I barely even remember writing it! :D – kumarharsh Aug 04 '16 at 13:37
3
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");