how to use logging module to log an object like print()

Vinay Sajip vinay_sajip at yahoo.co.uk
Wed Oct 29 14:17:52 CET 2008


On Oct 29, 11:24 am, Steve Holden <st... at holdenweb.com> wrote:

>
> One deficiency of this approach, however, is that the string formatting
> is performed even when nologgingis required, thereby wasting a certain
> amount of effort on unnecessary formatting.
>

Though you can mitigate this using the pattern:

if logger.isEnabledFor(logging.DEBUG):
    logger.debug("Message with variable data which may be expensive:
%s", expensive_call())

Which will only make the expensive_call() and formatting when the
logging call will actually do something.

Regards,

Vinay Sajip



More information about the Python-list mailing list