
On 08/09/2015 04:01 AM, Stefan Behnel wrote:
Sure, I use formatting there. But the formatting is intentionally done *after* checking that the output passes the current log level. The proposal is about providing a way to format a string literal *before* anyone can do something else with it. So it won't help for logging. Especially not for debug logging.
This discussion reminds me of a debate I had with a co-worker last year. I think I argued "your" side on that one, Stefan. He insisted on writing log lines like this: log.debug('File "{filename}" has {lines} lines.'.format(filename=filenames, lines=lines) #etc He said this form was most readable, because you could ignore the right side. While I said we should log like this, not only because it's shorter, but also because the formatting doesn't happen unless the log level is reached: log.debug('File "%s" has %s lines.', filename, lines) I also argued on performance grounds, but when I tried to prove it in real applications the difference was almost nothing, perhaps because the logger has to check a few things before deciding to format the string. Logging from a tight loop probably would create more overhead, but we've rarely done that. So performance didn't turn out to be a good reason to chose in most cases. In a tight loop you could still use isEnabledFor(level) for example. This experience did inform my original feature request, the result is now shorter, more readable, and the performance hit is negligible: log.debug(f'File "{filename}" has {lines} lines.') Also, my coworker and I would be able to move on to the next argument. ;) Another feature request would be to have logging support .format syntax like it does printf syntax, anyone know why that never happened?
output = "writing {filename} ...".format( filename=self.build_printable_relative_filename(filename))
rather than having to say
printable_filename = self.build_printable_relative_filename(filename) output = f"writing {printable_filename} ..." # magic happening here del printable_filename # not used anywhere else
This is where I disagree. Because if I have an important variable that I am using and bothering to log, such as a filename, I undoubtedly am going to use it again soon, to do an operation with it. So, I'll want to keep that variable around to use it again, rather than doing a recalculation. Cheers, -Mike