how to use logging module to log an object like print()
timr at probo.com
Fri Oct 31 07:56:37 CET 2008
Steve Holden <steve at holdenweb.com> wrote:
>Diez B. Roggisch wrote:
>> Steve Holden schrieb:
>>> Diez B. Roggisch wrote:
>>>> davy zhang schrieb:
>>>>> logger.debug("debug message",d)#can not do this
>>>> logger.debug("yes you can: %r", d)
>>> One deficiency of this approach, however, is that the string formatting
>>> is performed even when no logging is required, thereby wasting a certain
>>> amount of effort on unnecessary formatting.
>> Nope, it's not. It only happens when the message is really logged.
>Vinay, please tell me whether I was right or wrong ...
If you will forgive me for putting words into your mouth, Steve, I suspect
that you originally read the line as this:
logger.debug("yes you can: %r" % d)
instead of what was actually written:
logger.debug("yes you can: %r" , d)
With the %, of course the formatting will be done whether or not the string
is logged. With the comma, the string formatting is done inside the logger
module, and will be skipped if the message is not needed.
This is exactly why logger supports this somewhat unusual feature. Now,
one can certainly imagine circumstances where the parameter evaluation is
expensive, so Vinay's bypass is still useful in some circumstances.
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the Python-list