fine grain logging cotrol

Eric S. Johansson esj at
Sat Mar 24 16:29:34 CET 2007

Dennis Lee Bieber wrote:
> On Fri, 23 Mar 2007 21:15:56 -0400, "Eric S. Johansson" <esj at>
> declaimed the following in comp.lang.python:
>> Gabriel Genellina wrote:
>>> I don't get all the details of what's all that stuff for, but from the  
>>> error and traceback, I think you forgot to create the filter_test  
>>> instance. That is, change lgr.addFilter(filter_test) to  
>>> lgr.addFilter(filter_test())
>> do'h . for some reason, I thought addFilter took a class instead of an 
>> instance.  but on testing, I'm still missing something and getting the 
>> same error
> 	Doesn't your filter_test class require a parameter itself -- the
> "name"?
yes.  here is the code that fails.  I don't understand why the unbound 
method.  what is really weird is that when I singlestep through the 
code, it never seems to call filter_test.  it is like the method never 

  File "C:\Python24\lib\logging\", line 539, in filter
     if not f.filter(record):
TypeError: unbound method filter() must be called with filter_test 
instance as first argument (got LogRecord instance instead)

import logging
import sys

class filter_test (logging.Filter):
     test_names = { "Felis.alpha" : True,
                    "Catus.alpha" : False,

     def ___init__ (self, name):
         """simple filter test """
         logging.Filter.__init__(self, name)
         self.test_name = name

     def filter(self, record):
         """test and forget """
         return test_names.has_key( self.test_name) and test_names[ 
self.test_name ]

class LoggedType(type):
     def __new__(mcl, name, bases, classdict):
         def get_logger(self):
             tag = "%s.%s" % (name,sys._getframe(1).f_code.co_name)
             lgr = logging.getLogger(tag)
             fl = filter_test(tag)
             return lgr

         classdict["_%s__logger" % name] = property(get_logger)
         return type.__new__(mcl, name, bases, classdict)

class Logged:
     __metaclass__ = LoggedType

class Felis(Logged):
     def alpha(self):"Felis.alpha")
     def gamma(self):"Felis.gamma")

class Catus(Felis):
     def alpha(self):"Catus.alpha")
     def beta(self):"Catus.beta")

if __name__ == "__main__":
         format="EXPECTED %(message)s GOT %(name)s", level=logging.INFO)
     f = Felis()
     c = Catus()

More information about the Python-list mailing list