Why use `log.info("x=%s", x)` rather than an f-string in log calls?
IntermediateConceptExceptions, context managers, files & logging
Answer
With %s arguments, formatting only happens if the record is actually emitted, so a log.debug in a hot loop costs almost nothing when DEBUG is off. Aggregators like Sentry also group records by the unformatted template, so "failed to delete %s" is one issue rather than 10,000 unique ones. ruff rule G004 flags f-strings in logging calls.
More on Exceptions, context managers, files & logging
How does `try` / `except` / `else` / `finally` work?BeginnerWhy is a bare `except:` (or `except Exception: pass`) dangerous?BeginnerWhat is exception chaining, and what is `raise ... from`?IntermediateWhen should you define custom exception classes?IntermediateWhat is the EAFP vs LBYL style, and which does Python prefer?IntermediateHow does a `with` statement work, and why use it for files?Beginner