-
-
Notifications
You must be signed in to change notification settings - Fork 211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add LogfmtRenderer #376
Add LogfmtRenderer #376
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, this is great thank you! logfmt doesn't seem to be too well-maintained these days, but we can give it a try before we implement it ourselves.
As you've suspected, we need to make logfmt optional, I've added instructions to the pyproject.toml lines
Tasks that I couldn't add to the diff:
- add test env with logfmt to tox.ini (analogous to the rich env)
- add a usage example to
typing_examples.py
Um, so I've had a look at logfmt (of course one second after sending the review 🤦♂️) and I don't think we need a dependency for 17 LoC? def format_line(extra):
outarr = []
for k, v in extra.items():
if v is None:
outarr.append("%s=" % k)
continue
if isinstance(v, bool):
v = "true" if v else "false"
elif isinstance(v, numbers.Number):
pass
else:
if isinstance(v, (dict, object)):
v = str(v)
v = '"%s"' % v.replace('"', '\\"')
outarr.append("%s=%s" % (k, v))
return " ".join(outarr) |
Co-authored-by: Hynek Schlawack <hs@ox.cx>
for more information, see https://pre-commit.ci
Implement the rendering logic directly according to "specification" at https://pkg.go.dev/github.com/kr/logfmt
Thanks for the fast and detailed review!
Agreed. I added the logfmt rendering logic to Since there's no formal specification, I oriented myself to the behavior of the go and node implementations. The go docs have an example and a pseudo-spec. I also quickly checked that Grafana's LogQL
I guess that this is obsolete with the dependency to
I added a block similar to the ones for |
Thanks for the quick turnaround!
Wonderful, looks great! We might want to add a
yep
it just uses the APIs and looks whether mypy is happy or not. the code is never executed. |
Summary
This PR adds a
LogfmtRenderer
processor to format log lines in the logfmt format.The core of the work is done by the
logfmt
library, which is added as global dependency. Or should this be changed to feature-based optional?Refactored the code in
KeyValueRenderer
to share thesort_keys
/key_order
/drop_missing
logic. Tests are directly adapted fromKeyValueRenderer
.Closes #322
Pull Request Check List
This is just a friendly reminder about the most common mistakes. Please make sure that you tick all boxes. But please read our contribution guide at least once, it will save you unnecessary review cycles!
If an item doesn't apply to your pull request, check it anyway to make it apparent that there's nothing left to do.
docs/api.rst
by hand.versionadded
,versionchanged
, ordeprecated
directives. Find the appropriate next version in our__init__.py
file.If you have any questions to any of the points above, just submit and ask! This checklist is here to help you, not to deter you from contributing!