Root logger#
There is a default logger - root
logger. This page consiters how you can manipulate with it.
import logging
Note that you can access the root logger just by calling logging.getLogger
and manipulate it just like with a regular logger.
logging.getLogger()
<RootLogger root (WARNING)>
Format#
To set up the format for the basic logger, you should use the format
parameter of the logging.basicConfig
function.
The following example sets up basic logger to print the time of the event and it’s message.
logging.basicConfig(format='%(asctime)s %(message)s')
logging.debug("A DEBUG Message")
logging.info("An INFO")
logging.warning("A WARNING")
logging.error("An ERROR")
logging.critical("A message of CRITICAL severity")
2024-11-20 11:13:36,098 A WARNING
2024-11-20 11:13:36,099 An ERROR
2024-11-20 11:13:36,099 A message of CRITICAL severity
To check format of the basic logger you need:
Use
logging.getLogger()
to get the basic logger;Grab the handler you’re interested in. By default, basic loggers have only one handler;
The
formatter._fmt
field of the handler would be a string defining the format.
The following cell prints the notebooks handler format.
basic_logger = logging.getLogger()
print(basic_logger.handlers[0].formatter._fmt)
%(asctime)s %(message)s
You cannot update the format by calling basicConfig
again. The following cell tries to do that:
logging.basicConfig(format="%(levelname)s => %(message)s")
print(basic_logger.handlers[0].formatter._fmt)
logging.error("hello")
2024-11-20 11:13:40,474 hello
%(asctime)s %(message)s
Even a direct _fmt
change does not allow to change the output format:
basic_logger.handlers[0].formatter._fmt = "%(levelname)s => %(message)s"
logging.error("hello")
2024-11-20 11:13:42,621 hello