[Twisted-Python] logging
Jamu Kakar
jkakar at kakar.ca
Fri Jun 14 00:41:18 MDT 2013
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:
>
> def msg(format_string, *args):
> try:
> line = format_string % args
> except:
> # something clever
>
> No regexes involved, and very cheap in the case where it works.
>
> And this is a personal opinion: the "working way" (where programmed
> coded all ok) should be as cheap as possible. If something is wrong
> with the logging line, it must not crash, and I don't care if it's
> expensive, but that line will be fixed, and will work ok, and from
> there to the future it will be cheap.
You might be interested in logfmt (which has implementations for Ruby,
Javascript and Go at least):
https://github.com/jkakar/logfmt-python
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.
Thanks,
J.
More information about the Twisted-Python
mailing list