On this page
logging.Filter
Prev: logging.FileHandler | Up: logging | Next: logging.Formatter
Rel: logging.addLevelName, logging.basicConfig, logging.BufferingFormatter, logging.captureWarnings
Ref: docs.python.org — logging.Filter
Tags: #public #python3 #stdlib #logging
Import
import logging
from logging import Filter
Signature & Overview
logging.Filter(name='')
Filter instances are used to perform arbitrary filtering of LogRecords.
Methods & Attributes
Filter.filter(self, record)— Determine if the specified record is to be logged.
Usage
import logging
class DropHealthcheck(logging.Filter):
def filter(self, record: logging.LogRecord) -> bool:
return "/health" not in record.getMessage()
f = DropHealthcheck()
r1 = logging.LogRecord("web", logging.INFO, "", 1, "GET /health", (), None)
r2 = logging.LogRecord("web", logging.INFO, "", 1, "GET /api", (), None)
assert f.filter(r1) is False and f.filter(r2) is True
Gotchas
- Filters on a
Loggerdon't apply to propagated child records: Attach filters to aHandlerif you want them to screen records from descendant loggers too. - Can also enrich records: Mutate
record.request_id = ...insidefilter()and returnTrue.
REPL Inspect
from logging import Filter
help(Filter)
Linked from
Related notes
- tuts/python3/stdlib_module/logging/FileHandler Linked here; links to this page; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/Formatter Linked here; links to this page; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/logging/addLevelName Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/basicConfig Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/BufferingFormatter Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/captureWarnings Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/critical shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/debug shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/disable shared tags: #logging, #stdlib