[Twisted-Python] logging

Christopher Armstrong radix at twistedmatrix.com
Fri Jun 14 13:45:50 MDT 2013

On Fri, Jun 14, 2013 at 1:41 AM, Jamu Kakar <jkakar at kakar.ca> wrote:
> Hi,
> On Thu, Jun 13, 2013 at 6:41 PM, Facundo Batista
> <facundobatista at gmail.com> wrote:
>> Mmm... which is the problem with doing:
>> log.msg("Foo happened. Relevant information includes: "
>>              "bar=%s baz=%s boz=%s", bar, baz, boz)
>> Is way more readable, and in the practice the code will just need to
>> do something like:
> The example above could be written in a logfmt-compatible way as:
>   log.msg("message=Foo happened bar=%s baz=%s boz=%s", bar, baz, boz)
> This is as readable as what you have above while being a touch easier
> to parse.  The main point that logs are data and should emitted in a
> way that makes consuming them easy is a good one regardless of the
> format you choose.  I do wonder if logfmt is unnecessary and if simply
> dumping JSON objects would be just as good... logfmt is slightly
> prettier but the JSON approach has the advantage of having a parser
> available in basically every language anyone would ever care about.

I think both of these should be avoided because they require the user
to specify string interpolation themselves. I'd prefer to avoid that
entirely, by default.

log.msg("Foo happened**", bar=bar, baz=baz, boz=boz)

This is better because:

1. it keeps the data structured
2. it is a lot less typing

** I would prefer this part to be "foo", but I'll keep these
discussions separate :)

Christopher Armstrong

More information about the Twisted-Python mailing list