Python Glossary Русский

all Python 3loggingterm

logging.NullHandler

handlers

A handler that does nothing. A library attaches it to its own logger so it does not impose any output on the application.

Syntax

logging.getLogger('mylib').addHandler(logging.NullHandler())

When no handler exists anywhere in the logger chain, logging prints WARNING and above to stderr through the lastResort fallback; NullHandler counts as a handler and switches that off. So attach it only to your library's logger, never to the root — or the application loses every warning. It does not stop propagation: handlers configured by the application still receive the records.

Examples

import logging
lib = logging.getLogger('mylib')
print(lib.hasHandlers())  # → False — сработал бы lastResort
lib.addHandler(logging.NullHandler())
print(lib.hasHandlers())  # → True
lib.warning('в stderr ничего не попадёт')

See also

Open in the full glossarydocs.python.orgReport an inaccuracy