Samiul Alam
Samiul Alam

Reputation: 2444

What is logging levels?

I know transports are the places where I want to keep my logs but I don't understand what is the level of logging? I have the following code to create a logger with multiple means of transport.

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

When I want to log, logger.log('error', err), it logs in both info.log and error.log files. Why is this happening? Can somebody explain the idea of levels in logging, please?

Upvotes: 0

Views: 1809

Answers (1)

Elliot Nelson
Elliot Nelson

Reputation: 11557

Geno's comment is correct; in pretty much all logging platforms (winston, log4js, etc.), the log level represents the maximum level of error to print.

Setting log level to ERROR means "only print FATAL and ERROR messages".

Setting log level to INFO means "print FATAL, ERROR, WARN, and INFO messages".

There is no way (in Winston, at least, but I think it's generally true across the board) to specify a log transport that only carries INFO messages and not ERROR messages. This is by design.

When you set a log level, you are actually specifying a level of detail - FATAL is the least detailed logging, DEBUG is the most detailed logging. It wouldn't make sense to ask for more detail, and then have fatal errors disappear from the log. That is why every error level also includes all messages from levels "below" it.

Upvotes: 1

Related Questions