[Python-checkins] r85733 - in python/branches/py3k: Doc/library/logging.rst Lib/logging/__init__.py

vinay.sajip python-checkins at python.org
Tue Oct 19 23:13:49 CEST 2010


Author: vinay.sajip
Date: Tue Oct 19 23:13:49 2010
New Revision: 85733

Log:
logging: Documented usage of callables ans filters.

Modified:
   python/branches/py3k/Doc/library/logging.rst
   python/branches/py3k/Lib/logging/__init__.py

Modified: python/branches/py3k/Doc/library/logging.rst
==============================================================================
--- python/branches/py3k/Doc/library/logging.rst	(original)
+++ python/branches/py3k/Doc/library/logging.rst	Tue Oct 19 23:13:49 2010
@@ -3040,12 +3040,12 @@
 Filter Objects
 --------------
 
-:class:`Filter`\ s can be used by :class:`Handler`\ s and :class:`Logger`\ s for
-more sophisticated filtering than is provided by levels. The base filter class
-only allows events which are below a certain point in the logger hierarchy. For
-example, a filter initialized with "A.B" will allow events logged by loggers
-"A.B", "A.B.C", "A.B.C.D", "A.B.D" etc. but not "A.BB", "B.A.B" etc. If
-initialized with the empty string, all events are passed.
+``Filters` can be used by ``Handlers`` and ``Loggers`` for more sophisticated
+filtering than is provided by levels. The base filter class only allows events
+which are below a certain point in the logger hierarchy. For example, a filter
+initialized with "A.B" will allow events logged by loggers "A.B", "A.B.C",
+"A.B.C.D", "A.B.D" etc. but not "A.BB", "B.A.B" etc. If initialized with the
+empty string, all events are passed.
 
 
 .. class:: Filter(name='')
@@ -3068,6 +3068,15 @@
 will not be filtered by a logger's filter setting, unless the filter has also
 been applied to those descendant loggers.
 
+.. versionchanged:: 3.2
+
+You don't need to create specialized ``Filter`` classes: you can use a plain
+function (or other callable) as a filter. The filtering logic will check to
+see if the filter object has a ``filter`` attribute: if it does, it's assumed
+to be a ``Filter`` and its :meth:`~Filter.filter` method is called. Otherwise,
+it's assumed to be a callable and called with the record as the single
+parameter. The result should conform to that of :meth:`~Filter.filter`.
+
 Other uses for filters
 ^^^^^^^^^^^^^^^^^^^^^^
 

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 23:13:49 2010
@@ -613,13 +613,8 @@
         for f in self.filters:
             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
+                result = f(record) # assume callable - will raise if not
             if not result:
                 rv = 0
                 break


More information about the Python-checkins mailing list