Python logging: loggers, handlers, and the root logger
Answer: Python's logging package implements a flexible event logging system that applications and libraries can share.
Loggers, handlers, filters, and formatters
The module defines functions and classes that implement event logging for applications and libraries.
Idiomatic usage creates a module level logger and uses that logger for logging.
Those messages travel to handlers on higher loggers, and finally to the root logger, which is hierarchical logging.
Logging is useful only after you configure it, including the levels and destinations for each logger.
In most cases only the root logger needs to be so configured, since lower level loggers forward their messages to it.
basicConfig() provides a quick way to configure the root logger for many use cases.
Loggers are the interface application code calls.
Handlers send the records that loggers create to the right destination.
Filters provide a finer grained facility for determining which log records to include.
Formatters specify the layout of log records in the final output.
How to obtain a logger
Do not construct Logger objects yourself; call the module-level logging.getLogger(name) function instead.
Repeated getLogger() calls with the same name return the same Logger object.
A logger name is often a period-separated hierarchy, similar to a Python package path, and it can also be a plain name.
Loggers further down in the hierarchical list are children of loggers higher up, and all loggers are descendants of the root logger.
If you name loggers with __name__ on a per-module basis, the logger tree matches the package tree.
See also
Claims
- id: C1 claim: Application code should call getLogger rather than construct Logger objects. source: S1 evidence: "Loggers should NEVER be instantiated directly"
- id: C2 claim: getLogger returns the same object when called again with the same name. source: S1 evidence: "Multiple calls to getLogger() with the same name will always return a reference"
- id: C3 claim: Loggers should never be constructed directly; call getLogger instead. source: S2 evidence: "Loggers should never be instantiated directly"
Sources
- [S1] logging — Logging facility for Python. Python Software Foundation. https://docs.python.org/3/library/logging.html . License: PSF License Version 2. Accessed 2026-11-26.
- [S2] logging documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/logging.rst . License: PSF License Version 2. Accessed 2026-11-26.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, logging — Logging facility for Python; CPython, logging documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, logging — Logging facility for Python; CPython, logging documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python logging: loggers, handlers, and the root logger." iinventedlight Library, iil:note:284kvc1f, version of 2026-11-26. https://iinventedlight.com/library/notes/python-logging/. License: CC-BY-4.0.
@misc{iil_note_284kvc1f,
author = {{iinventedlight}},
title = {{Python logging: loggers, handlers, and the root logger}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-26},
url = {https://iinventedlight.com/library/notes/python-logging/},
note = {Permanent ID iil:note:284kvc1f. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-logging.schema.json (schema; CC-BY-4.0)
- python-logging.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-logging.md.