I'm running into a performance problem in a project, and I narrowed it down to some of the log lines. It seems that f-strings are calculated even when my logging facility is above the level of the line that is logging.
Consider this example to demonstrate the issue:
import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger('MyLogger') class MyClass: def __init__(self, name: str) -> None: self._name = name def __str__(self) -> str: print('GENERATING STRING') return self._name c = MyClass('foo') logger.debug(f'Created: {c}')
When this example is run, I get "GENERATING STRING" printed to screen, indicating that the __str__
method is being ran even though my logging level is set to INFO
and the log line is for DEBUG
.
From what I can tell today, the solution is to use the following vs an f-string.
logger.debug('Created: %s', c)
There are three things going through my head right now.
I'm curious to know what others do in this situation. Is the %s
the best (most modern) approach? Is there a more modern way that I should be logging as demonstrated above?
I have a lot of code to update (fix), and I'm hoping to align with modern best practices.
As of Python 3.6, f-strings are a great new way to format strings. Not only are they more readable, more concise, and less prone to error than other ways of formatting, but they are also faster!
In Python, the built-in logging module can be used to log events. Log messages can have 5 levels - DEBUG, INGO, WARNING, ERROR and CRITICAL. They can also include traceback information for exceptions. Logs can be especially useful in case of errors to help identify their cause.
To define basic logging needs, several lines of code are needed. Including additional logging information is not easy. The print() statement only displays messages on the console. Recording logging data inside a file or sending it over the internet needs additional works.
The documentation says that the logging lib is optimized to use the %s
formatting style. I can't remember where it is mentionned exactly, but I read it a few months ago.
Edit - Found! https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles
Edit2 - (thanks to Robin Nemeth): https://docs.python.org/3/howto/logging.html#optimization
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With