fine grain logging cotrol

Eric S. Johansson esj at harvee.org
Fri Mar 23 23:34:31 CET 2007


Peter Otten wrote:
> Eric S. Johansson wrote:
> 
> Here is yet another revision of my example then:

it's making more and more sense although I don't quite follow 'property' 
quite yet.  But I see that get_logger is invoked prior to the 
__logger.info call.

I was looking at how to implement one of my other requirements 
(conditional based on a predicate) and I see that I could use a filter. 
    I've experimented a little but come up empty.  This is what I changed:

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

     def ___init__ (self, name):
         """simple filter test """
         self.name = name

     def filter(self, record):
         """test and forget """
         return test_names.has_key(self.name) and test_names[self.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)
             lgr.addFilter(filter_test)
             return lgr

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

It's probably real obvious but I keep getting a:

   File "C:\Python24\lib\logging\__init__.py", 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)


I'm puzzled that I thought I was adding the filter object at the right 
point in time with addFilter.  I wouldn't be surprised if I was totally 
off base but I'd be interested in figuring out how I went off the rails 
so I can fill in the knowledge.

---eric






More information about the Python-list mailing list