customizing a logging logger

Vinay Sajip vinay_sajip at yahoo.co.uk
Thu Sep 13 08:24:03 EDT 2007


On 12 Sep, 19:17, garyjefferson... at gmail.com wrote:
> 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?

You're almost right - it's __str__() rather than __repr__() that gets
called, when str() is called on the msg object passed in the logging
call. The str() call happens in LogRecord.getMessage(), which is
called (indirectly) from a handler's emit() when the message needs to
be formatted.

So - I don't think that the other methods will provide better
performance, as this approach is the simplest and most direct. I
haven't done any tests to confirm this, though :-(

Note that in order to avoid computation of *other* data passed to the
logging call - i.e. other arguments - you should still use the
logger.isXXXEnabled() idiom to avoid unnecessary computation, where
performance is an issue.

Best regards,


Vinay Sajip




More information about the Python-list mailing list