Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jan 14, 2015 at 19:28 | history | edited | the Tin Man | CC BY-SA 3.0 | edited body |
| Oct 21, 2014 at 19:49 | comment | added | David Sanders | You can defer formatting with any kind of formatting style. You're not restricted to python modulo formatting. The logging module converts objects to strings before emitting log messages. Therefore, any object that implements__str__ will do. That could include a class which accepts a new-style formatting string with its parameters but doesn't perform the operation until the logging module calls__str__. See my answer below. | |
| Feb 14, 2014 at 11:03 | comment | added | trojjer | If you're concerned about performance, use literal dict {} syntax instead of a dict() class instantiation:doughellmann.com/2012/11/… | |
| Oct 10, 2013 at 12:26 | comment | added | Jonathan Hartley | Oh! I see. I took your meaning backwards. Beg your pardon. Yep, I could imagine an aggregator that tried to do that, but it seems silly. Hugs! | |
| Oct 10, 2013 at 12:24 | comment | added | Wooble | No, I meant that a sufficiently sophisticated system could just find the similarities and group them together even if you use string formatting. Not that I'm aware of whether any can actually do this. | |
| Oct 10, 2013 at 12:22 | comment | added | Jonathan Hartley | Hey @Wooble. I don't see how. Doing this is absolutely trivial (i.e. can be implemented in the aggregator in about three characters, literally), and if it doesn't do this, then in what sense is it a useful aggregator? | |
| Oct 10, 2013 at 10:12 | comment | added | Wooble | @JonathanHartley: depends on how sophisticated your aggregator is. | |
| Oct 10, 2013 at 8:04 | comment | added | Jonathan Hartley | The primary benefit of this is not performance (doing the string interpolation will be quick compared to whatever you're doing with the output from logging, e.g displaying in a terminal, saving to disk) It is that if you have a logging aggregator, it can tell you "you got 12 instances of this error message", even if they all had different 'some_info' values. If the string formatting is done before passing the string to log.debug, then this is impossible. The aggregator can only say "you had 12 different log messages" | |
| Mar 12, 2013 at 10:17 | comment | added | smci | @snth I just split the OP's question into two separate questions. But this good answer unifies both. | |
| Jan 30, 2013 at 21:55 | comment | added | Cito | @VinaySajip: Cool, thanks for the info and sorry for spreading misinformation. It's also in the "What's new in 3.2" article here:docs.python.org/3.2/whatsnew/3.2.html#logging. Very useful. | |
| Jan 30, 2013 at 19:56 | comment | added | Vinay Sajip | @Cito: See this:plumberjack.blogspot.co.uk/2010/10/… | |
| Nov 25, 2012 at 17:00 | comment | added | Cito | You can use dict formatting like this:log.debug("some debug info: %(this)s and %(that)s", dict(this='Tom', that='Jerry')) However, you can't use the new style.format() syntax here, not even in Python 3.3, which is a shame. | |
| Nov 14, 2012 at 7:36 | comment | added | snth | This is some useful info that I just learned now. It's a pity it doesn't have it's own question as it seems separate to the main question. Pity the OP didn't split his question in two separate questions. | |
| Feb 22, 2011 at 19:21 | history | answered | Wooble | CC BY-SA 2.5 |