[Python-checkins] r85730 - in python/branches/py3k: Lib/logging/__init__.py Lib/test/test_logging.py Misc/NEWS
vinay.sajip
python-checkins at python.org
Tue Oct 19 22:53:01 CEST 2010
Author: vinay.sajip
Date: Tue Oct 19 22:53:01 2010
New Revision: 85730
Log:
logging: Allowed filters to be just callables.
Modified:
python/branches/py3k/Lib/logging/__init__.py
python/branches/py3k/Lib/test/test_logging.py
python/branches/py3k/Misc/NEWS
Modified: python/branches/py3k/Lib/logging/__init__.py
==============================================================================
--- python/branches/py3k/Lib/logging/__init__.py (original)
+++ python/branches/py3k/Lib/logging/__init__.py Tue Oct 19 22:53:01 2010
@@ -604,10 +604,23 @@
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
Modified: python/branches/py3k/Lib/test/test_logging.py
==============================================================================
--- python/branches/py3k/Lib/test/test_logging.py (original)
+++ python/branches/py3k/Lib/test/test_logging.py Tue Oct 19 22:53:01 2010
@@ -309,6 +309,35 @@
finally:
handler.removeFilter(filter_)
+ def test_callable_filter(self):
+ # Only messages satisfying the specified criteria pass through the
+ # filter.
+
+ def filterfunc(record):
+ parts = record.name.split('.')
+ prefix = '.'.join(parts[:2])
+ return prefix == 'spam.eggs'
+
+ handler = self.root_logger.handlers[0]
+ try:
+ handler.addFilter(filterfunc)
+ spam = logging.getLogger("spam")
+ spam_eggs = logging.getLogger("spam.eggs")
+ spam_eggs_fish = logging.getLogger("spam.eggs.fish")
+ spam_bakedbeans = logging.getLogger("spam.bakedbeans")
+
+ spam.info(self.next_message())
+ spam_eggs.info(self.next_message()) # Good.
+ spam_eggs_fish.info(self.next_message()) # Good.
+ spam_bakedbeans.info(self.next_message())
+
+ self.assert_log_lines([
+ ('spam.eggs', 'INFO', '2'),
+ ('spam.eggs.fish', 'INFO', '3'),
+ ])
+ finally:
+ handler.removeFilter(filterfunc)
+
#
# First, we define our levels. There can be as many as you want - the only
Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS (original)
+++ python/branches/py3k/Misc/NEWS Tue Oct 19 22:53:01 2010
@@ -34,6 +34,8 @@
Library
-------
+- logging: Allowed filters to be just callables.
+
- logging: Added tests for _logRecordClass changes.
- Issue #10092: Properly reset locale in calendar.Locale*Calendar classes.
More information about the Python-checkins
mailing list