Winston: Create logger with a transport with lower log level than log level of logger: unexpected behaviour?

Created on 18 Feb 2018  路  5Comments  路  Source: winstonjs/winston

I create a logger with level=info, but I add a transport using `level=debug'. Three files are created:

  • error.log: contains only error level logs. this is expected behavior right?
  • debug.log: contains error, warn, info, verbose and debug logs
  • combined.log: contains errors up until info as expected

I expected error.log to only contain debug level logs because I assumed the top level info is filtered for debug only for that transport. But why then does my transport for debug contain all other logs up until debug? Is this expected or a bug?

const winston = require('winston');
const logger = winston.createLogger({
    level: 'info',
    format: winston.format.json(),
    transports: [
        new winston.transports.File({ filename: 'error.log', level: 'error' }),
        new winston.transports.File({ filename: 'debug.log', level: 'debug' }),
        new winston.transports.File({ filename: 'combined.log' }),
    ],
});

logger.error('error');
logger.warn('warn');
logger.info('info');
logger.verbose('verbose');
logger.debug('debug');
logger.silly('silly');
faq

Most helpful comment

From the readme:

A logger accepts the following parameters:
level [..] Log only if聽info.level聽less than or equal to this level

And description for e.g. log level for console transport:

level: Level of messages that this transport should log (default 'info').

I also expected the level on the logger object to be a kind of filter, and no matter what the transports are, they will never get anything above this level.

If this is just a default for all transport levels of that logger that's fine - but it could be documented better :)

All 5 comments

You are setting maximum reported level for each transport. The most severe messages have the lowest level (emerg = 0), and the least important, the higher the level is (debug = 7).

That being said - if you set one transport to error (3) level and other to debug (7), first one will get only error and lower (more severe) messages, whilst the other one will get debug, info, warn, error... and so on. One transport doesn't filter out the other one.

The level at the logger level is the default that can be overriden at the transport level.

For me everything seems to work as expected.

https://github.com/winstonjs/winston#using-logging-levels

From the readme:

A logger accepts the following parameters:
level [..] Log only if聽info.level聽less than or equal to this level

And description for e.g. log level for console transport:

level: Level of messages that this transport should log (default 'info').

I also expected the level on the logger object to be a kind of filter, and no matter what the transports are, they will never get anything above this level.

If this is just a default for all transport levels of that logger that's fine - but it could be documented better :)

If this is just a default for all transport levels of that logger that's fine - but it could be documented better :)

I agree!

@jacob87o2 thanks for clarification, I actually forgot about setting a maximum, so my brain fart that wanted to have debug only was maybe causing some confusion... anyways I was expecting the logger containing the transports to filter, but knowing that it is a default setting clarifies things for me!

don't know if you want to close this now, or keep this open until a decision/action is taken.

Standardizing on [RFC5424] in winston is by design. There are a few open documentation tickets for this. PRs welcome 鈥撀爄t's a tough subject to explain succinctly.

Was this page helpful?
0 / 5 - 0 ratings