The logging Module Basics
Configure loggers, set levels, and emit messages properly.
Why logging over print?
The logging module provides severity levels, configurable output destinations, timestamps, and the ability to silence output without code changes.
import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug("debug message")
logging.info("info message")
logging.warning("warning message")
logging.error("error message")
logging.critical("critical message")Log Levels
Python defines five standard levels in increasing severity: DEBUG, INFO, WARNING, ERROR, CRITICAL. Only messages at or above the configured level are emitted.
import logging
# Numeric values:
# DEBUG = 10
# INFO = 20
# WARNING = 30
# ERROR = 40
# CRITICAL = 50
logging.basicConfig(level=logging.WARNING)
logging.debug("hidden") # not shown
logging.warning("shown") # shownAll lessons in this course
- The logging Module Basics
- Handlers, Formatters, and Filters
- Structured Logging
- Debugging with pdb