Description
I recently learned that I've been using the logging
module incorrectly. The problem is that I've been using the pattern
logger.debug('array of numbers that you rarely want to see: {}'.format(numbers))
Regardless of the logging level, numbers
will be formatted into a string. This is because the string formatting is part of the input to the logging function, so it happens before the logging module has a chance to decide if it actually needs to do the conversion and print the output.
The correct way to do it is
logger.debug('array of numbers that you rarely want to see: %s', numbers)
Now numbers
is only converted into a string when the logging level is set to debug
.
This only makes a measurable impact in function time in a few places, but I want to change it throughout the library so that people don't learn bad habits from my examples.
I had some utility functions in some other code that spent 75% of their time doing string formatting for rare logging calls instead of doing real work. Embarrassing.