Open huonw opened 2 months ago
Thank you for the detailed issue!
I think it's a little bit complicated than that. The formatting is decided by the Formatter objects which can use %
but it can also use other formatting style which is determined by the style
parameter and it can be set separately for each logger or can be set through the global config.
Or, maybe we can just check if the string uses %
-style format characters and make an assumption that it uses the %
style.
Thanks for the quick reply!
I'm a little confused by the ins-and-outs of logging
at times... but, I think there's two layers of formatting:
LogRecord
's message, using the values passed to info
/error
/etc (msg
& args
) to set message
.Formatter
like you say. (And its style
arg to customise.)For 1, I believe this happens unconditionally using %
, unless there's some way to use a LogRecord
subclass/replacement. See code: https://github.com/python/cpython/blob/9728ead36181fb3f0a4b2e8a7291a3e0a702b952/Lib/logging/__init__.py#L391-L401
In addition, there's existing linting rules that explicitly suggest turning f-strings and .format
calls into %
, so "use %
for logging" is an assumption with precedent:
What do you think?
There's two rules (F509, PLE1300) that detect when a
%
formatted string include an invalid format character. However, these don't seem to flag format strings passed to logging functions (debug
,info
, etc.). These strings use%
for formatting in the same way.(References: "The message attribute of the record is computed using msg % args" https://docs.python.org/3/library/logging.html#logging.Formatter.format , https://github.com/python/cpython/blob/9728ead36181fb3f0a4b2e8a7291a3e0a702b952/Lib/logging/__init__.py#L391-L401).
Reproducer: https://play.ruff.rs/36d5756e-44c7-48a1-8d8e-ae45a29ee480
Settings (note
"select": ["ALL"]
)To confirm that this is an error, comment out the
x = ...
line and run the code. It prints:Issues that are (somewhat) related:
Thanks for ruff, very speedy!