Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.7 logging: f-strings vs % [duplicate]

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.

  • Most of the examples and docs I read seem to be pretty old.
  • This project is Python 3.7+ only (not worried about being backwards compatible).
  • I have a lot of lines of code to update.

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.

like image 874
theG Avatar asked Jan 25 '19 15:01

theG


People also ask

Are F-strings faster Python?

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!

What are the five levels of logging in Python?

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.

What is the difference between print and logging in Python?

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.


1 Answers

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

like image 63
olinox14 Avatar answered Sep 23 '22 13:09

olinox14