customizing a logging logger

garyjefferson123 at gmail.com garyjefferson123 at gmail.com
Sat Aug 18 06:53:34 CEST 2007


Suppose I have some sort of context variable that I want to appear in
log messages.  E.g.:

logger = logging.getLogger("somelogger")
class SomeOp:
    def __init__(self, ctx):
        self.ctx = ctx
    def method1(self):
        logger.info("%s: here's a message", self.ctx)

What's the idiomatic way to abstract away the inclusion of self.ctx
from the calls in logger.info() et al?  Is there some way I can
declare a

    @property
    def info(self): return logger.info

but have it insert the '"%s: " % self.ctx' bit for me in one place
instead of the dozens of places I currently do it in the class?

Thanks,
Gary




More information about the Python-list mailing list