Summary
The Python logging module has a “handler of last resort” that prints log messages to sys.stderr when no other handlers are configured. However, deactivating this handler by setting logging.lastResort to None does not prevent log messages from being printed.
Root Cause
The root cause of this behavior is that the logging.lastResort handler is not the only mechanism that prints log messages to sys.stderr. When a log message is emitted, Python’s logging module will attempt to find a handler to process the message. If no handlers are configured, the message will be printed to sys.stderr, regardless of the state of logging.lastResort.
Why This Happens in Real Systems
This behavior occurs in real systems because the logging module is designed to provide a fallback mechanism for printing log messages when no other handlers are configured. This ensures that important log messages are not lost, even if the logging configuration is incomplete or incorrect.
Real-World Impact
The real-world impact of this behavior is that deactivating the logging.lastResort handler does not necessarily prevent log messages from being printed to sys.stderr. This can lead to unexpected behavior and make it more difficult to diagnose logging issues.
Example or Code
import logging
logging.info("hello")
logging.warning("hello")
logging.lastResort = None
logging.warning("hello")
How Senior Engineers Fix It
Senior engineers fix this issue by understanding the underlying mechanics of the Python logging module and configuring the logging system correctly. This may involve setting up custom handlers, configuring the logging level, and ensuring that the logging configuration is complete and correct.
Why Juniors Miss It
Junior engineers may miss this issue because they do not fully understand the Python logging module and its fallback mechanisms. They may assume that deactivating the logging.lastResort handler will prevent log messages from being printed, without realizing that other mechanisms are at play.