[Python-ideas] Efficient debug logging

Barry Scott barry at barrys-emacs.org
Wed Feb 15 06:47:11 EST 2017


On Wednesday, 15 February 2017 22:33:42 GMT Chris Angelico wrote:
> On Wed, Feb 15, 2017 at 10:18 PM, Steven D'Aprano <steve at pearwood.info> 
wrote:
> > Python doesn't have thunks, but there is a relatively heavyweight
> > solution for delayed evaluation: wrap the code in a function.
> > 
> > debugLog( ‘info is %r’, lambda: expensiveFunction() )
> > 
> > 
> > and then adjust debugLog so that if the argument is a function, it will
> > call the function only when needed:
> > 
> > def debugLog(message, value):
> >     if debug_log_enabled:
> >         if isinstance(value, types.FunctionType):
> >             value = value()
> >         
> >         log(message % value)
> 
> Or use the function as the __repr__ of some object, which comes to the
> same thing without requiring special code inside debugLog.

__repr__ is interesting however.

Typically I describe in a string why the value is being logged. That means
that the object is always a string. I cannot recall using debugLog( obj )
in production.

dlog('This is the state of obj at the start of event processing: %r' % (obj,))

Barry



More information about the Python-ideas mailing list