问题
I would like logging.info()
to go to journald (systemd).
Up to now I only found python modules which read journald (not what I want) or modules which work like this: journal.send('Hello world')
回答1:
python-systemd has a JournalHandler you can use with the logging framework.
From the documentation:
import logging
from systemd.journal import JournalHandler
log = logging.getLogger('demo')
log.addHandler(JournalHandler())
log.setLevel(logging.INFO)
log.info("sent to journal")
回答2:
An alternative to the official package, the systemd package works with python 3.6. Its source is also on github.
The implementation is a mirror of the official lib, with some minor changes:
import logging
from systemd import journal
log = logging.getLogger('demo')
log.addHandler(journal.JournaldLogHandler())
log.setLevel(logging.INFO)
log.info("sent to journal")
or for an even shorter method:
from systemd import journal
journal.write("Hello Lennart")
来源:https://stackoverflow.com/questions/34588421/how-to-log-to-journald-systemd-via-python