[x] I think this refactoring is useful for everyone (if it's too specific, consider a custom rule)
[x] I have checked there are no similar issues suggesting the same thing
Description
The in-built Python logging module has its own optimizations:
Formatting of message arguments is deferred until it cannot be avoided.
We should make use of these optimizations when using the logging module, instead of f-strings, string concatenation, string formatting (via the % operator) or str.format().
Note: The variable types may have to be taken into account for different variables, I'm not sure if just using %s will convert it into its __str__ format.
Code Before
import logging
a = "world"
b = "goodbye"
c = 1
d = 2
logging.info(f"Hello {a}")
logging.debug(b + " Tom!")
logging.warning(f"c = {c}")
logging.log("d = %d" % d)
Code After
import logging
a = "world"
b = "goodbye"
c = 1
d = 2
logging.info("Hello %s", a)
logging.debug("%s Tom!", b)
logging.warning("c = %d", c)
logging.log("d = %d", d)
Checklist
Description
The in-built Python logging module has its own optimizations:
We should make use of these optimizations when using the logging module, instead of f-strings, string concatenation, string formatting (via the % operator) or
str.format()
.Note: The variable types may have to be taken into account for different variables, I'm not sure if just using
%s
will convert it into its__str__
format.Code Before
Code After