Hacker News new | ask | show | jobs
by gbajson 2587 days ago
The example of f-string in the article is quite unfortunate, it suggests using it for logging messages. F-strings shouldn't be used in loggers, because they aren't lazy evaluated.
3 comments

TBF for the vast majority of logging it doesn't really matter. Not to mention the laziness is only the (usually cheap) formatting itself. If you're logging the result of non-trivial expressions (which would otherwise not be computed at all) you have to handroll it.

f-strings are mostly unusable for translatable contexts. And format-strings also don't work then (they're a security issue).

I already experienced performance issue, just because log messages (millions/s) were evaluated, for logger in debug mode, so it really depends on the system.
A cookie for this man!
What instead?
Standard logging functions, without f-strings. So instead of: logging.debug(f'Problem with {x}') use: logging.debug('Problem with %s', x).