Python Logging: Levels, Formatters, Handlers, Rotating Files, Structured Logging, and Every Pattern Data Engineers Need in Production
Complete Python logging guide for data engineers. Why not print() (5 reasons with side-by-side comparison). Getting started with basicConfig and named loggers. The 5 standard log levels (DEBUG through CRITICAL with hospital triage analogy and when-to-use table). Formatters (10 built-in attributes table, dev vs prod vs debug format examples). Handlers (StreamHandler for console, FileHandler for files, multiple handlers with mail delivery analogy, RotatingFileHandler for size-based rotation, TimedRotatingFileHandler for date-based rotation). Named loggers and hierarchy (dot-separated names, propagation, per-module control). Complete reusable configure_logging function. Module-level and class-level logger patterns using __name__. Logging exceptions (logger.exception vs exc_info=True). Structured JSON logging (custom JSONFormatter class, extra context, LoggerAdapter for persistent fields). Three configuration methods (basicConfig, code-based, dictionary config with per-module levels). Five data engineering patterns: pipeline run logger with run_id, step timer context manager with auto slow-query warnings, DataFrame metrics logger (rows, nulls, dupes, memory), error quarantine logger to separate file, multi-table pipeline with full summary. 7 common mistakes and 6 interview Q&As.