Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>IMHO, for messages that are very likely to be displayed, such as those given to <code>error</code> or <code>warn</code> it does not make much of a difference.</p> <p>For messages that are less likely displayed, I would definitely go for the second version, mainly for performance reasons. I often give large objects as a parameter to <code>info</code>, which implement a costly <code>__str__</code> method. Clearly, sending this pre-formatted to <code>info</code> would be a performance waste.</p> <p><strong>UPDATE</strong></p> <p>I just checked the source code of the <code>logging</code> module and, indeed, formatting is done <em>after</em> checking the log level. For example:</p> <pre><code>class Logger(Filterer): # snip def debug(self, msg, *args, **kwargs): # snip if self.isenabledfor(debug): self._log(debug, msg, args, **kwargs) </code></pre> <p>One can observe that <code>msg</code> and <code>args</code> are untouched between calling <code>log</code> and checking the log level.</p> <p><strong>UPDATE 2</strong></p> <p>Spired by Levon, let me add some tests for objects that have a costly <code>__str__</code> method:</p> <pre><code>$ python -m timeit -n 1000000 -s "import logging" -s "logger = logging.getLogger('foo')" -s "logger.setLevel(logging.ERROR)" "logger.warn('%s', range(0,100))" 1000000 loops, best of 3: 1.52 usec per loop $ python -m timeit -n 1000000 -s "import logging" -s "logger = logging.getLogger('foo')" -s "logger.setLevel(logging.ERROR)" "logger.warn('%s' % range(0,100))" 1000000 loops, best of 3: 10.4 usec per loop </code></pre> <p>In practice, this could give a fairly high performance boost.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload