Python’s logging framework is powerful, but gives you plenty of ways to shoot yourself in the foot. To make matters worse, logging mistakes are subtle and slip through code review easily.
In my time at Venmo, I’ve seen logging mistakes cause everything from unneeded debugging to application crashes. Here are the most common culprits:
This pushes the interpolation off to the logging framework:
Pylint will detect this antipattern as W1201 and W1202.
Formatting an exception throws away the traceback, which you usually want for debugging.
logger.exception is a helper that calls
logger.error but also logs the traceback.
%s with a unicode string will cause it to be encoded, which will cause an
EncodingError unless your default encoding is utf-8.
%r will format the unicode in \u-escaped repr format instead.