diff options
author | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2010-10-19 20:53:01 (GMT) |
---|---|---|
committer | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2010-10-19 20:53:01 (GMT) |
commit | 6dbed2e8b3fb90b25e2b99c5e522a5205626d07e (patch) | |
tree | 76ceee4dbccb88e720729fa53428838981429998 /Lib/logging | |
parent | 6fac8171367e0f2626613eb0ac07fc79c25f2500 (diff) | |
download | cpython-6dbed2e8b3fb90b25e2b99c5e522a5205626d07e.zip cpython-6dbed2e8b3fb90b25e2b99c5e522a5205626d07e.tar.gz cpython-6dbed2e8b3fb90b25e2b99c5e522a5205626d07e.tar.bz2 |
logging: Allowed filters to be just callables.
Diffstat (limited to 'Lib/logging')
-rw-r--r-- | Lib/logging/__init__.py | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 995d313..551d85e 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -604,10 +604,23 @@ class Filterer(object): The default is to allow the record to be logged; any filter can veto this and the record is then dropped. Returns a zero value if a record is to be dropped, else non-zero. + + .. versionchanged: 3.2 + + Allow filters to be just callables. """ rv = 1 for f in self.filters: - if not f.filter(record): + if hasattr(f, 'filter'): + result = f.filter(record) + elif hasattr(f, '__call__'): + try: + result = f(record) + except Exception: + result = True # filter failed, assume a pass + else: + result = False # we don't know what f is + if not result: rv = 0 break return rv |