Search code examples
pythonloggingsystemdpython-logging

How to log to journald (systemd) via Python?


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


Solution

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