Sub topic 7
The logger hierarchy and propagation
This chapter assumes you can create loggers with getLogger and attach handlers to them. It explains how Python connects loggers to each other and how a record travels between them.
Dots define the tree
Logger names form a tree, and the dots in a name mark the levels. The logger myapp.db is a child of myapp. myapp is a child of the root logger, which has no name. logging.getLogger() with no argument returns the root logger.
You never declare the tree. Calling getLogger("myapp.db") creates the logger and links it to its nearest existing ancestor. Using getLogger(__name__) in each module gives you a tree that matches your package layout.
Effective level
A new logger has level NOTSET, which means it has no opinion. When a logger decides whether to handle a record, it uses its effective level. That is its own level if one is set. Otherwise it is the level of the nearest ancestor that has one. The root logger defaults to WARNING.
Propagation
After a record passes the level check on the logger where it was created, the logger passes it to its own handlers. Then, if propagate is True (the default), it passes the record to the parent logger's handlers, and so on up to the root.
import logging
logging.basicConfig(level=logging.INFO)
parent = logging.getLogger("myapp")
child = logging.getLogger("myapp.db")
parent.setLevel(logging.WARNING)
print(child.getEffectiveLevel() == logging.WARNING)
child.info("Dropped: the child inherits WARNING from its parent")
child.warning("Shown: propagates to the root logger's handler")The script prints True, then one line, WARNING:myapp.db:Shown: propagates to the root logger's handler. The info call is dropped because the child's effective level is WARNING.
This is why you usually attach handlers only to the root logger. Every record from every module then reaches them.
Stopping propagation
Set propagate = False when a logger has its own handlers and its records should not also reach the root. Otherwise each message appears twice.
import logging
logging.basicConfig(level=logging.INFO, format="root: %(message)s")
audit = logging.getLogger("myapp.audit")
audit.setLevel(logging.INFO)
audit.addHandler(logging.StreamHandler())
audit.propagate = False
audit.info("Printed once, by the audit handler only")If you remove the last-but-one line, the message prints twice. The audit handler prints it, and the root handler prints it again with the root: prefix.
Common errors
- Duplicate lines. A handler on both a child and an ancestor, with propagation on, prints each record twice.
- Silent loggers. A record is dropped because an ancestor's level is higher than the call. Check
getEffectiveLevel()first. - Missing output. A logger with no handlers anywhere in its chain falls back to a last-resort handler. That handler prints only
WARNINGand above, without formatting.
Exercise
The logger "app" has level ERROR. The logger "app.net" has no level set, and the root logger has a handler at level DEBUG. Does logging.getLogger("app.net").warning("slow") reach that handler? Why or why not?
Show answer
No. "app.net" inherits the effective level ERROR from "app", so the WARNING call fails the level check on the logger where it was made. The record is dropped before it propagates, and the root handler's DEBUG level is never consulted.