reubano / pygogo

A Python logging library with superpowers
MIT License
281 stars 20 forks source link

Add support for logfmt #7

Open foobacca opened 7 years ago

foobacca commented 7 years ago

logfmt (second article) is a format meant to be between JSON and normal logs for (human) readability and (machine) parse-ability.

Any chance it could be added to pygogo? I might have a go myself, but thought I'd ask at least.

reubano commented 7 years ago

Would you mind posting an example of the desired usage and output?

offero commented 6 years ago

It's structured like JSON but a bit more readable. Plenty of examples in that posted link.

offero commented 6 years ago

https://pypi.python.org/pypi/logfmt/0.2

reubano commented 6 years ago

Based on the links, the clearest example I saw was from here:

foo=bar a=14 baz="hello kitty" cool%story=bro f %^asdf

You can get something like that with the custom logger:

import pygogo as gogo

kwargs = {'f': True}
extra = {'foo': 'bar, 'a': 14, 'baz': 'hello kitty', 'story': 'bro'}
logfmt = 'foo="%(foo)s" a="%(a)s" baz="%(baz)s" cool_story="%(story)s" "%(message)s"'

fmtr = logging.Formatter(logfmt)
logger = gogo.Gogo('custom', low_formatter=fmtr).get_logger(**kwargs)
logger.debug('%^asdf', extra=extra)

# Prints the following to `stdout`:

foo=bar a=14 baz="hello kitty" cool_story=bro %^asdf

Is that what you are talking about?

offero commented 6 years ago

Yes, except everything is key value, even the message (message="%^asdf"). And there has to be smart quoting "" for spaces and strings with quotes.

reubano commented 6 years ago

@offero, unfortunately, the link I posted above doesn't match what you just said. They don't display keys for the last two values. Can you please show a complete usage example for what you are looking to achieve? Thanks!