Python: Optimize logging of expensive debugging operations
Formatting of message arguments is deferred until it cannot be avoided. However, computing the arguments passed to the
loggingmethod can also be expensive, and you may want to avoid doing it if theloggerwill just throw away your event. To decide what to do, you can call theisEnabledFor()method which takes a level argument and returnsTrueif the event would be created by theLoggerfor that level of call. You can write code like this:
if logger.isEnabledFor(logging.DEBUG):
logger.debug("Message with %s, %s", expensive_func1(), expensive_func2())
so that if the
logger’s threshold is set aboveDEBUG, the calls toexpensive_func1()andexpensive_func2()are never made.
Via docs.python.org.
Leave a comment