0Pricing
Python Academy · Lesson

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")  # shown

All lessons in this course

  1. The logging Module Basics
  2. Handlers, Formatters, and Filters
  3. Structured Logging
  4. Debugging with pdb
← Back to Python Academy