fine grain logging cotrol

Peter Otten __peter__ at web.de
Fri Mar 23 13:31:52 EDT 2007


Eric S. Johansson wrote:

> Peter Otten wrote:
>> Eric S. Johansson wrote:
>> 
>> [in private mail -- please don't, Eric]
> 
> sorry.  my preference is for private mail.  it's my way of trying to be
> kind to others by reducing list clutter.

It is not list clutter in my book; it gives others the chance to correct,
add to, or even profit from our conversation.

>> I don't understand. The logging package detects the function name without
>> user intervention.
 
> not in 2.4 afaik.  

Hmm, I must have overread that constraint in your previous posts...

Here is yet another revision of my example then:

import logging
import sys

class LoggedType(type):
    def __new__(mcl, name, bases, classdict):
        def get_logger(self):
            return logging.getLogger("%s.%s" % (name,
sys._getframe(1).f_code.co_name))
        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()

Peter




More information about the Python-list mailing list