sourcery icon indicating copy to clipboard operation
sourcery copied to clipboard

Taking into account logging optimisations

Open TheMythologist opened this issue 3 years ago • 1 comments

Checklist

  • [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)

TheMythologist avatar Sep 10 '22 01:09 TheMythologist

Oh yeah these look useful!

Hellebore avatar Sep 11 '22 17:09 Hellebore