structlog
structlog copied to clipboard
Feature: journal output option
This might be a nice thing to add- a JournalLogger class:
https://github.com/systemd/python-systemd
Cool project! I wish I used more Python so I could make use of it.
What exactly would be necessary for that? I have no access to a systemd system but wouldn’t it just require a convenience factory? The examples look like there is no need for adapting/formatting at all.
I bet you do by now ;-)
Almost! :) We’re in the process of upgrading to Xenial and logging is a part of that. I hope to get around this ASAP.
@hynek - glad to see you're alive, tried to say hi on irc but tumbleweed...
heh just came back from PyCon ZA
Daiquiri's JournalHandler code is a good example of how that could be done. It's 38 lines of code, so it's not that complicated.
Essentially it boils down to just journal.send(message, {"KEY1": "value1", "KEY2": "value2"}
, but with the quirk that it needs uppercase keys, and that there are some special well-known keys (like PRIORITY
) that would be good to support.
Also, the systemd module provides a logging.handler JournalHandler that is reasonable. So one can rely on the logging module for this.
This example is gross, but works:
logging.basicConfig(format="%(message)s", handlers=(systemd.journal.JournalHandler(),))
structlog.configure(
processors=[
structlog.stdlib.add_log_level,
lambda _, __, d: {k.upper() if k != "event" else k: v for k, v in d.items()},
structlog.stdlib.render_to_log_kwargs,
],
context_class=structlog.threadlocal.wrap_dict(dict),
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
)
I wonder if we should add this to http://www.structlog.org/en/stable/logging-best-practices.html?
At this point I think this should be an external package. I don't care about this, it adds a dependency, and the headaches I have from standard library logging are bad enough.
I'll be happy to help anyone, who would like to tackle that.
To be fair, it can be implemented internally, without an external dependency.
Right let it put me this way then: If someone shows up with a good PR that doesn't introduce too much complexity, I will consider merging it. Either way, if someone cares enough, they can publish it separately if I decline, so there's no risk to do work in vain.
I do not want to have it in my public todo list anymore, though.
Oh and one last thing: in my other projects I've started experimenting with GitHub discussions which is a bit better medium for cases like this. If y'all who care about this want to talk/coordinate you can of course do it here; I'm not locking the issue. But I could also be convinced to activate Discussions for structlog. I don't want to stop anyone from communicating; I just want to manage my blood pressure.