I'm using the python logging library with python 3.9.
I want to avoid the expense of doing a decode
operation as most times I won't have debug level logging enabled.
stdout, _ = process.communicate()
logging.debug("stdout: %s", stdout.decode('utf-8'))
One solution to avoid the decode
call when logging is disabled is to explicitly check isEnabledFor
, however, this adds a lot of noise to the code that I would like to avoid.
if logging.getLogger().isEnabledFor(logging.DEBUG):
logging.debug("stdout: %s", stdout.decode('utf-8'))
I'm hoping there's a string formatting option that will defer the decode
operation and be easy to read.
logging.debug("stdout: %(bs.utf-8)", stdout)
Does such an operation or formatter exist?