fine grain logging cotrol
Eric S. Johansson
esj at harvee.org
Sat Mar 24 11:29:34 EDT 2007
Dennis Lee Bieber wrote:
> On Fri, 23 Mar 2007 21:15:56 -0400, "Eric S. Johansson" <esj at harvee.org>
> 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
existed.
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)
------------------
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)
lgr.addFilter(fl)
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):
self.__logger.info("Felis.alpha")
def gamma(self):
self.__logger.info("Felis.gamma")
class Catus(Felis):
def alpha(self):
self.__logger.info("Catus.alpha")
def beta(self):
self.__logger.info("Catus.beta")
if __name__ == "__main__":
logging.basicConfig(
format="EXPECTED %(message)s GOT %(name)s", level=logging.INFO)
f = Felis()
f.alpha()
f.gamma()
c = Catus()
c.alpha()
c.beta()
c.gamma()
More information about the Python-list
mailing list