structlog icon indicating copy to clipboard operation
structlog copied to clipboard

Feature: journal output option

Open philips opened this issue 10 years ago • 8 comments

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.

philips avatar Sep 17 '13 19:09 philips

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.

hynek avatar Sep 18 '13 09:09 hynek

I bet you do by now ;-)

cjw296 avatar Oct 11 '16 05:10 cjw296

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 avatar Oct 13 '16 07:10 hynek

@hynek - glad to see you're alive, tried to say hi on irc but tumbleweed...

cjw296 avatar Oct 13 '16 09:10 cjw296

heh just came back from PyCon ZA

hynek avatar Oct 13 '16 10:10 hynek

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.

paravoid avatar Feb 27 '20 22:02 paravoid

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,
    )

paravoid avatar Feb 28 '20 00:02 paravoid

I wonder if we should add this to http://www.structlog.org/en/stable/logging-best-practices.html?

hynek avatar Feb 29 '20 09:02 hynek

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.

hynek avatar Oct 09 '23 09:10 hynek

To be fair, it can be implemented internally, without an external dependency.

andrewshadura avatar Oct 09 '23 11:10 andrewshadura

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.

hynek avatar Oct 09 '23 14:10 hynek

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.

hynek avatar Oct 09 '23 14:10 hynek