Log Level Design and Rotation:... Note

Log Level Design and Rotation: Why Apps Use DEBUG/INFO/WARNING/ERROR

Application logs use varying labels like DEBUG, INFO, WARNING, and ERROR, which primarily function as filtering thresholds. Python's logging module defines five levels, from DEBUG (10), representing fine-grained detail, to CRITICAL (50), indicating application failure. Setting a logger's level to INFO means only messages with a numeric value of 20 or higher will be recorded. This allows developers to embed detailed diagnostic statements without cluttering normal operation logs, activating them only when needed for investigation. The example application, maintenance_agent.py, routes log output to a RotatingFileHandler and a StreamHandler, initially set to INFO, so DEBUG messages are typically suppressed. The distribution of log calls in the application reflects its design assumptions, with INFO for progress, WARNING for recoverable issues, and ERROR for genuine failures. CRITICAL is unused as the application is designed to handle site-specific failures independently. Log rotation, implemented by RotatingFileHandler, prevents log files from growing indefinitely by setting a maximum size and backup count. This mechanism ensures log history is preserved without consuming excessive disk space. A separate handler, _SiteLogCapture, collects ephemeral logs for individual site maintenance runs to generate reports or emails, then discards them. This demonstrates how multiple handlers can process the same log stream for different purposes: long-term history versus temporary, specific use. Log levels act as a vertical filter for detail, while handler choices serve as a horizontal filter for audience and purpose.