Is it advantageous to call logging functions with format string + args list vs. formatting inline?
I've seen (and written) logging code that uses inline string formatting:
logging.warn("%s %s %s" % (arg1, arg2, arg3))
and yet I assume it's better (performance-wise, and more idiomatic) to use:
logging.warn("%s %s %s", arg1, arg2, arg3)
because the second form avoids string formatting operations prior to invoking the logging function. If the current logging level would filter out the log message, no formatting is necessary, reducing computing time and memory allocations.
Am I on the right track here, or have I missed something?
In case this is helpful, here is a quick timing test for just the two formatting options:
seems to give the second approach the edge.
Avoiding inline string formatting does save some time if the current logging level filters the log message (as I expected) -- but not much:
So, as user1202136 pointed out, the overall performance difference depends on how long it takes to format the string (which could be significant depending on the cost of calling
__str__
on arguments being passed to the logging function.)IMHO, for messages that are very likely to be displayed, such as those given to
error
orwarn
it does not make much of a difference.For messages that are less likely displayed, I would definitely go for the second version, mainly for performance reasons. I often give large objects as a parameter to
info
, which implement a costly__str__
method. Clearly, sending this pre-formatted toinfo
would be a performance waste.UPDATE
I just checked the source code of the
logging
module and, indeed, formatting is done after checking the log level. For example:One can observe that
msg
andargs
are untouched between callinglog
and checking the log level.UPDATE 2
Spired by Levon, let me add some tests for objects that have a costly
__str__
method:In practice, this could give a fairly high performance boost.