all Python 3loggingterm
logging.Logger
loggers
The logger class: a program writes its log through its debug(), info() and warning() methods. Instances come from getLogger(), not the constructor.
Syntax
logger = logging.getLogger(name); logger.setLevel(level); logger.info(msg, *args)
Logger('x') makes an orphan outside the hierarchy: it has no parent, never reaches the root's handlers and is not the object getLogger('x') returns. The logger level and the handler level are two separate sieves a record must pass, so setLevel(DEBUG) on a logger shows nothing if the handler has a higher level set (a fresh handler is NOTSET and lets everything through). propagate=False stops records from bubbling up to ancestors — the usual cure for duplicated lines.
Examples
import logging
a = logging.getLogger('app')
print(a is logging.getLogger('app'), logging.Logger('app') is a) # → True False
print(logging.Logger('app').parent) # → None
import logging, sys
log = logging.getLogger('app')
h = logging.StreamHandler(sys.stdout)
h.setLevel(logging.INFO)
log.addHandler(h)
log.setLevel(logging.DEBUG)
log.debug('не видно: отсеял обработчик')
log.info('видно') # → видно
print(log.getEffectiveLevel()) # → 10
See also
Open in the full glossarydocs.python.orgReport an inaccuracy