Python Glossary Русский

all Python 3loggingterm

logging.Filter

records and filters

A log record filter: by default it passes only records from the logger with the given name and its descendants.

Syntax

flt = logging.Filter(name=''); handler.addFilter(flt)

Names are matched along the dotted hierarchy, not as a string prefix: Filter('app') passes 'app.db' but not 'application'. A filter attached to a logger never sees records propagating up from child loggers — attach it to a handler to filter everything. Any callable taking a record works as a filter, and since 3.12 it may return a modified record instead of True.

Examples

import logging
f = logging.Filter('app')
for name in ('app', 'app.db', 'application'):
    print(name, bool(f.filter(logging.makeLogRecord({'name': name}))))  # → app True / app.db True / application False
import logging, sys
h = logging.StreamHandler(sys.stdout)
h.addFilter(lambda r: 'пароль' not in r.getMessage())  # функция тоже фильтр
log = logging.getLogger('demo')
log.addHandler(h)
log.warning('пароль: 123')
log.warning('вход выполнен')  # → вход выполнен

See also

Open in the full glossarydocs.python.orgReport an inaccuracy