Python logging filters let an application decide which records reach a logger or handler. After OSPython.032: Custom Log Formatting, the next practical step is learning to select records without changing their display format. A filter can enforce a precise policy, such as accepting records from one subsystem or excluding known-noisy messages.
Learning objectives
Explain where filters run, distinguish a level threshold from a custom filter, implement a filter with the standard library, test it, and identify common mistakes.
What a filter does
A logging.Filter object exposes a filter(record) method that returns a truthy value to retain a log record or a false value to discard it. Logger filters act on events logged directly to that logger; they do not automatically filter every record from descendants during propagation. Handler filters are applied to records processed by that handler. A logger or handler can have multiple filters, all of which must allow a record.
Example: keep only one subsystem
The following small example configures a stream handler with a custom filter that accepts records only when their logger name begins with app.network. The prefix check includes child loggers such as app.network.dns.
import logging
class NetworkOnly(logging.Filter):
def filter(self, record):
return record.name == 'app.network' or record.name.startswith('app.network.')
handler = logging.StreamHandler()
handler.addFilter(NetworkOnly())
logging.basicConfig(level=logging.DEBUG, handlers=[handler], force=True)
logging.getLogger('app.network.dns').info('DNS ready')
logging.getLogger('app.storage').info('Disk ready')
Only the DNS message reaches this handler. In an application with multiple handlers, a separate handler may still process the storage message. Use Python’s logging filter documentation for precise behavior, including newer Python versions where filters may return a replacement LogRecord.
Filters versus severity levels
A handler level such as WARNING selects messages based on severity; a custom filter can inspect logger names, attributes, or message metadata. Filters are not a substitute for protecting secrets: avoid logging passwords, access tokens, or private data in the first place. Keep filtering deterministic and lightweight because it runs on the logging path.
Corey Schafer’s advanced Python logging tutorial demonstrates named loggers, handlers, and formatters—the components to which custom filters are attached.
Lab
Create two named loggers and one stream handler. First, emit INFO and WARNING records without a custom filter. Next, attach NetworkOnly to the handler and repeat. Add a second handler without the filter to compare their output. Record which messages appear, and explain why. To extend the exercise, allow only records with an explicitly added component attribute.
Knowledge check
1. What return value rejects a record? 2. Does a filter on a parent logger automatically filter propagated child records? 3. How does a handler level differ from a custom filter?
Answers
1. A false value. 2. No; attach filtering to the appropriate handler or descendant loggers. 3. A handler level uses severity, while a custom filter can inspect record attributes.
References
Read Python logging documentation and revisit OSPython.028: Python Logging Basics. Next rotation subject: Linux.

Leave a Reply