f-string in a log call
f-string in a log call. Formats the log message before knowing it will be logged.
This is how the comment reads inline, under the flagged line.
Why it matters
f-strings run even when the level is off, and they defeat log grouping because every message is unique.
What to do
logger.info("saved %s", user_id).
Examples
logger.info(f"saved {user_id}")logger.info("saved %s", user_id)What it catches
It reads a pattern on each line, aware of strings and comments. Languages: Python. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
logger.info(f"saved {u}")
Left alone
logger.info("saved %s", u)
Research
f-strings inside log calls. The string is built even when the level is off, and structured logging loses its fields.
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"fstring-in-logger"
],
"severity": {
"fstring-in-logger": "medium"
}
}
}