Interview Prep/Python for DevOps/Exceptions, context managers, files & logging

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.

All Python for DevOps questions Roadmap: Python for automation

More on Exceptions, context managers, files & logging