Logger/Handler level

Logger/Handler level#

You can set levers for both the handler and the logger. But what if they have different levels? Note that the messages with insufficient level will be rejected by the handler or the logger. So only records with a level higher than the level of the logger and the level of the handler will be logged.

import logging
from pathlib import Path
logger = logging.getLogger("test")

Basic example#

In the following cells, two options have been tried:

  • The logger level is higher than the handler level;

  • The logger level is lower than the handler level.

logger.setLevel("WARNING")

handler = logging.StreamHandler()
handler.setLevel("DEBUG")
handler.setFormatter(
    logging.Formatter("%(levelname)s - %(message)s")
)

logger.addHandler(handler)
logger.info("message")
logger.handlers.clear()
logger.setLevel("DEBUG")

handler = logging.StreamHandler()
handler.setLevel("WARNING")
handler.setFormatter(
    logging.Formatter("%(levelname)s - %(message)s")
)

logger.addHandler(handler)
logger.info("message")
logger.handlers.clear()

As you can see, nothing is logged in either case.

Use case#

It is good to think about it in this context: the logger determines the global logging level and will not let inappropriate messages to any handler, and handlers determine which messages they need and which among those that passed the logger.

Suppose we only want to see debugging messages in terminal output, but when we save to file we only care about more serious messages - warnings and more serious. And we have already finished debugging our application, so we only care about info messages and above, so we can set the INFO level for the logger and all sources will have only info and above messages.

The following code realises described setup:

stream_handler = logging.StreamHandler()
stream_handler.setLevel("DEBUG")
file_handler = logging.FileHandler(
    Path("logger_handler_level_files")/"log",
    mode="w"
)
file_handler.setLevel("WARNING")

logger.setLevel("INFO")
logger.addHandler(stream_handler)
logger.addHandler(file_handler)

logger.debug("debug")
logger.info("info")
logger.warning("warning")
logger.error("error")
logger.critical("critical")

logger.handlers.clear()
info
warning
error
critical

So in terminal we can see all the messages that the logger has allowed us to see. But let’s also check the file.

!cat logger_handler_level_files/log
warning
error
critical

Here are only important enough messages.