customizing a logging logger

garyjefferson123 at garyjefferson123 at
Wed Sep 12 20:17:51 CEST 2007

On Sep 12, 10:46 am, Vinay Sajip <vinay_sa... at> wrote:
> On 11 Sep, 17:14, garyjefferson... at wrote:
> > What is the best way to do this, so that I don't have to manually put
> > the self.ctx in the log string in hundreds of different places?
> Another way is to generate your log messages via a factory method
> which prepends the context: you can do this via a mixin, too.
> def log_message(self, msg):
>     return "%s: %s" % (self.ctx, msg)
> Note also that in recent versions, an optional "extra" parameter can
> be used, to pass info into the LogRecord.
> See
> which may be of some help - it's a similar use case.
> Best regards,
> Vinay Sajip

The email thread you pointed to was very informative, especially since
I was about to go down the road of creating one logger per context

Whichever method I choose, I am particularly concerned with the
following aspect:

I like to use the "logger.debug("msg %s %s", s1, s2)" format, as I
understand that when the logging for this log level is turned off, we
don't have to do the potentially expensive string building that a '%'
operator would imply.

It seems like using something other than a literal string in the
message is the way to go, since I assume that its __repr__() won't get
called unless the logger is actually going to log a message for it.
Is that right?

Are any of the other methods likely to provide as-good or better


More information about the Python-list mailing list