4 ms·
I was also confused by the expression evaluation thing. Reading between the lines, it seems like logger.debug("hello %s", foo) may be better than log
by bobbiechen 4y ago
I was also confused by the expression evaluation thing. Reading between the lines, it seems like
logger.debug("hello %s", foo)
may be better than
logger.debug(f"hello {foo}")
in the case when loglevel is higher than debug. In the first version, the final string does not have to be computed, while in the second version, we might construct the string and then do nothing since the loglevel is excluded.
- masklinn 4y agoThat's exactly it. Although this becomes more complicated because printf-style string formatting is not free (though it's the cheapest of all methods save fstrings if I remember correctly), and because python does not support lazy parameters if `foo` is a non-trivial expression odds are good it will far outcost either formatting.
- jrootabega 4y agoYou're also able to add additional log-specific processors to the log record in the first case.
- bbayles 4y agoThe first is better also because you can do things with loggers other than print out their contents. For example, suppose you had a statement like: logger.debug('Database error: %s', error_message) You probably have a logging handler that did the normal string. But you can also have one that keeps a count of how many `Database error: %s` hits there are (as opposed to `Network error: %s`) there are over time. Doing the string substitution would break this aggregation.