Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Event logging

A logger decides whether to accept an event at its severity and a handler chooses the output destination and threshold.

Version: Python 3.14; repository image, production runtime not attested

Syntax

logger = logging.getLogger(__name__)
logger.info('rows=%d', count)

A logger decides whether to accept an event at its severity and a handler chooses the output destination and threshold.

A formatter renders the record. Hierarchical names allow ancestor handlers to receive child events. propagate=False stops forwarding after local handlers, preventing duplicates from that path.

The logger accepts INFO and WARNING, but the handler writes only WARNING|rows=0.

import logging
parent=logging.getLogger('app')
child=logging.getLogger('app.jobs')
assert child.parent is parent
parent.setLevel(logging.WARNING)
assert not child.isEnabledFor(logging.INFO)
child.setLevel(logging.INFO)
assert child.isEnabledFor(logging.INFO)

Examples

import io
import logging

out = io.StringIO()
log = logging.getLogger("report")
log.setLevel(logging.DEBUG)
log.propagate = False
handler = logging.StreamHandler(out)
handler.setLevel(logging.WARNING)
handler.setFormatter(logging.Formatter("%(levelname)s|%(message)s"))
log.addHandler(handler)
log.info("rows=%d", 3)
log.warning("rows=%d", 0)
print(out.getvalue().strip())

Result

WARNING|rows=0

The logger accepts INFO and WARNING, but the handler writes only WARNING|rows=0.

Sources