Python logging perforamnce when level is higher

Question:

One advantage of using logging in Python instead of print is that you can set the level of the logging. When debugging you could set the level to DEBUG and everything below will get printed. If you set the level to ERROR then only error messages will get printed.

In a high-performance application this property is desirable. You want to be able to print some logging information during development/testing/debugging but not when you run it in production.

I want to ask if logging will be an efficient way to suppress debug and info logging when you set the level to ERROR. In other words, would doing the following:

logging.basicConfig(level=logging.ERROR)
logging.debug('something')

will be as efficient as

if not in_debug:
    print('...')

Obviously the second code will not cost because checking a boolean is fast and when not in debug mode the code will be faster because it will not print unnecessary stuff. It comes to the cost of having all those if statement though. If logging delivers same performance without the if statements is of course much more desirable.

Asked By: Phrixus

||

Answers:

There’s no need for you to check is_debug in your source code, because the Logging module already does that.

Here’s an excerpt, with some comments and whitespace removed:

class Logger(Filterer):
    def debug(self, msg, *args, **kwargs):
        if self.isEnabledFor(DEBUG):
            self._log(DEBUG, msg, args, **kwargs)

Just make sure you follow pylint guidelines on how to pass parameters to the logging function, so they don’t execute stuff before calling the logging code. See PyLint message: logging-format-interpolation

I first learned about it through pylint warnings, but here’s the official documentation that says to use % formatting and pass arguments to be evaluated later: https://docs.python.org/3/howto/logging.html#optimization

Answered By: Kenny Ostrom
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.