Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use multiple arguments to log instead of interpolation?

Python's logging functions allow you to pass them multiple arguments that they can interpolate for you. So you have a choice:

logger.info("Something %s this way comes!" % "wicked")

or

logger.info("Something %s this way comes!", "wicked")

But why choose one over the other? Is it simply a matter of letting errors happen in the logger as opposed to in the program that's being logged, or is there something else to it?

like image 967
kojiro Avatar asked Jul 23 '12 14:07

kojiro


1 Answers

It's a matter of performance :

  • When you do the interpolation in your code, this code will be executed each time you call the logger (event if the logger or the log level is not activated)
  • When you let the logger do the interpolation, it will do the interpolation only if the logger and log level is activated.
like image 73
Cédric Julien Avatar answered Nov 15 '22 16:11

Cédric Julien