customizing a logging logger

garyjefferson123 at gmail.com garyjefferson123 at gmail.com
Wed Sep 12 14:17:51 EDT 2007


On Sep 12, 10:46 am, Vinay Sajip <vinay_sa... at yahoo.co.uk> wrote:
> On 11 Sep, 17:14, garyjefferson... at gmail.com 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.
>
> Seehttp://mail.python.org/pipermail/patches/2007-January/021535.html
> 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
(eek!).

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
performance?

thanks,
Gary




More information about the Python-list mailing list