Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Logging events with Logger.debug, info, warning, and error

Choose a method on a named logger based on the severity of an event, and see how logger and handler settings determine which messages appear.

Version: Python 3.12

Syntax

logger = logging.getLogger(name)
logger.debug(msg, *args, **kwargs)
logger.info(msg, *args, **kwargs)
logger.warning(msg, *args, **kwargs)
logger.error(msg, *args, **kwargs)

In Python 3.12, logging.Logger.debug/info/warning/error refers to four instance methods. Get a named logger with logging.getLogger(name), commonly logging.getLogger(__name__) in a module. Asking for the same name again returns the same instance. Module-level functions such as logging.info() instead use the root logger and call basicConfig() if it has no handlers. Instance methods do not configure the root logger automatically.

In increasing severity, use debug() for diagnostic details (DEBUG), info() for routine events (INFO), warning() for a potential or unexpected problem while the program can still operate (WARNING), and error() when an operation failed (ERROR). Use warning(), not the deprecated warn(). Logging an exception at ERROR does not by itself re-raise that exception. The error() call can still raise an exception of its own, for example a KeyError if extra attempts to overwrite a reserved LogRecord field.

All four accept msg, *args, **kwargs. Pass values separately, as in logger.debug("Items scanned: %d", 3): logging applies % substitution when it formats the message. With no positional arguments after msg, it performs no substitution. The supported keyword options are exc_info (exception details), stack_info (current stack), stacklevel (which caller location is recorded), and extra (custom record fields).

The logger's effective level is a threshold: INFO drops DEBUG. At NOTSET, a named logger looks to its ancestors for a level; the root starts at WARNING by default. Handlers can apply their own thresholds and filters. When a record propagates to ancestor handlers, the ancestor loggers' own levels and filters are not checked again.

Examples

import logging
import sys

logging.basicConfig(
    level=logging.DEBUG,
    stream=sys.stdout,
    format="%(levelname)s %(name)s: %(message)s",
)
logger = logging.getLogger("inventory")

logger.debug("Items scanned: %d", 3)
logger.info("Inventory loaded")
logger.warning("Stock is low")
logger.error("Could not save inventory")

logger.setLevel(logging.INFO)
logger.debug("This line is filtered out")

Result

DEBUG inventory: Items scanned: 3
INFO inventory: Inventory loaded
WARNING inventory: Stock is low
ERROR inventory: Could not save inventory

Run the script in a fresh Python 3.12 process. `basicConfig()` sets the root level to `DEBUG` and sends output to `sys.stdout`. Four lines appear; after `logger.setLevel(logging.INFO)`, the final `DEBUG` message is filtered out.

Related lessons

  • Python · Lesson 24 · Intermediate · Version: 3

Sources