How to log to journald (systemd) via Python?

guettli picture guettli · Jan 4, 2016 · Viewed 19.8k times · Source

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

Answer

martineg picture martineg · Jan 4, 2016

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