Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

`logging.Logger.propagate`: forwarding log records

`propagate` controls whether a log record also reaches ancestor loggers' handlers. Setting it to `False` stops forwarding upward without disabling local handlers.

Version: Python 3.12

Syntax

logger.addHandler(handler)
logger.propagate = False  # Stop forwarding to ancestor handlers.
logger.propagate = True   # Allow forwarding to ancestor handlers.

In Python 3.12, propagate is an attribute of a logging.Logger instance, not a method. It defaults to True. Calling logger.addHandler(handler) attaches a handler directly to that logger without changing propagate.

Once an event passes the originating logger's effective level and filters, its record is offered to that logger's handlers first. With True, forwarding continues up the dotted-name hierarchy, potentially to the root logger. If any logger in that chain has propagate = False, its own handlers can still handle the record, but no handlers above it receive the record.

Forwarding does not recheck ancestor loggers' levels or filters. Each handler's own level and filters still apply. This is distinct from effective-level inheritance: if the originating non-root logger's level is NOTSET, its effective threshold is resolved through the hierarchy before the record is forwarded.

If a local handler and an ancestor handler write to the same destination, you may see duplicate entries. Configure handlers only on a suitable ancestor, or attach a local handler and set logger.propagate = False to isolate its output. False alone does not guarantee silence: if no handler is found along the path, logging.lastResort can emit records at WARNING or above.

Examples

import logging
import sys


def console_handler(label):
    handler = logging.StreamHandler(sys.stdout)
    handler.setLevel(logging.INFO)
    handler.setFormatter(logging.Formatter(f"{label}: %(message)s"))
    return handler


root = logging.getLogger()
root.setLevel(logging.ERROR)
root.addHandler(console_handler("root"))

logger = logging.getLogger("checkout")
logger.setLevel(logging.INFO)
logger.addHandler(console_handler("checkout"))

logger.propagate = True
logger.info("order received")

logger.propagate = False
logger.info("order saved")

Result

checkout: order received
root: order received
checkout: order saved

Both handlers print the first `INFO` record. The root handler accepts it even though the root logger itself is set to `ERROR`. After `False` is set, only the `checkout` handler prints the second record.

Related lessons

  • Python · Lesson 24 · Intermediate · Version: 3

Sources