[Twisted-Python] logging

Wilfredo Sánchez Vega wsanchez at wsanchez.net
Fri Jun 14 17:20:58 MDT 2013


Christopher Armstrong wrote:
> logger.msg("scheduled-compaction-failed")
   I'm confused.  You don't want to use English, but... why not change 
your hyphens to spaces and call it a day?  Also, why did it fail?

   Here's a fuller example, modified to fit the API I'm using:

from twisted.python.log import Logger

log = Logger()

try:
   scheduleCompaction(...)
except Exception as e:
   log.error("Scheduled compaction failed: {why}", why=e,
             id=2C09A260-184C-44F9-B11F-E12B26B26C9C)


   Some things to note about this:

   - `log = Logger()` does some magic so that log.namespace is the name 
of your module "spacecombat.server.db".  So, your "system" identifier is 
perhaps covered by that, with no typing.

   - I have a format string instead of a fixed string.  An observer 
emitting text can emit something informative.  I know you think that 
text logs aren't useful, but a lot of us do.  And you can use observers 
that ignore this format.  Maybe there's an argument for making the 
format optional...

   - Formatting isn't done here, so... cheap if you don't use it in any 
observers.

   - I added a GUID id argument since you seem keen, I think on a unique 
key to identify the code point at which the message is logged.  It's not 
used in the format, but an observer storing things in a DB could use 
that to take you straight to the relevant code, or identify multiple 
instances of that message, etc. if the format string isn't how you want 
to do that.

     -wsv

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://twistedmatrix.com/pipermail/twisted-python/attachments/20130614/b4d40b1d/attachment.html>


More information about the Twisted-Python mailing list