Logging
The `logging` module provides a flexible framework for emitting log messages from Python programs. It supports different severity levels, multiple output destinations, and configurable formatting. The guide walks through Basic Configuration, Loggers & Handlers, Formatters, Logging in Libraries, Best Practices. `logging.basicConfig(level=logging.INFO)` sets up a default handler that writes to stderr. Levels: `DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL`. Messages below the configured level are ignored. Configure once at application startup. `logging.getLogger(__name__)` creates a hierarchical logger. `FileHandler` writes to a file. `StreamHandler` writes to streams. `RotatingFileHandler` rotates logs by size. `TimedRotatingFileHandler` rotates by time. Add handlers with `logger.addHandler(handler)`. `Formatter(fmt, datefmt)` controls log output format. Common format: `"%(asctime)s - %(name)s - %(levelname)s - %(message)s"`. `datefmt` uses `strftime` syntax. Available attributes: `%(name)s`, `%(levelname)s`, `%(message)s`, `%(filename)s`, `%(lineno)d`. The guide is organized into 5 sections that build on each other, each pairing a prose explanation with runnable code. Browse the individual functions covered by this guide in the Logging Module category.