Python › Logging
Why is it recommended to use logging.getLogger(__name__) instead of the root logger in library and module code?
IntermediateAnswer
Using __name__ creates a logger named after the module's dotted path, forming a hierarchy that mirrors your package structure. This lets consumers of your code configure logging per-module (e.g., silence a noisy submodule while keeping others verbose) and makes log output traceable to its source without every module competing for the single root logger's configuration.
# in mypackage/database.py
import logging
logger = logging.getLogger(__name__) # 'mypackage.database'
def connect():
logger.debug('Connecting to database...')Real-world example A library author uses getLogger(__name__) throughout so downstream applications can selectively enable DEBUG logging just for 'mylib.network' while keeping 'mylib.parsing' at WARNING.