0

I'm trying to implement a logger containing two handlers:

  • a handler that sends all the log messages with logging.INFO severity and higher - to a log file.
  • a handler that sends all the logs messages with logging.ERROR severity and higher - to stdout.

However, my logger writes only logging.ERROR level logs and above to both the file and stdout.

From looking at the documentation's handler section and a previous question about this topic, it looks like I'm doing everything right - I'm setting the logger to the lowest log level and then set each handler to the level I want it to filter.

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(LOG_FORMAT, datefmt="%H:%M:%S")

file_logger = logging.FileHandler(filename=consts.LOGS_FILE_LOCATION)
console_logger = logging.StreamHandler(sys.stdout)

file_logger.setFormatter(formatter)
file_logger.setLevel(logging.INFO)
logger.addHandler(file_logger)

console_logger.setFormatter(formatter)
console_logger.setLevel(logging.ERROR)
logger.addHandler(console_logger)

What am I missing?

1 Answers1

0

The root logger always has an explicit level set (WARNING by default). Therefore it is not enough to set the level of the handlers - the level of the logger itself should be set to the lowest of the handler's levels, in my case:

logger.setLevel(logging.INFO)