Skip to content

Instantly share code, notes, and snippets.

@moreati
Created June 15, 2021 20:44
Show Gist options
  • Save moreati/5f12d177c0a5d6c4404df8d352d4b945 to your computer and use it in GitHub Desktop.
Save moreati/5f12d177c0a5d6c4404df8d352d4b945 to your computer and use it in GitHub Desktop.
Demonstration of logging to systemd journal with offcial Python bindings, and cysystemd
#!/usr/bin/env python3
import logging
log = logging.getLogger('systemd_demo')
log.setLevel(logging.DEBUG)
# Official bindings
import systemd.journal
systemd.journal.send(
"Hello systemd",
PRIORITY=systemd.journal.LOG_DEBUG,
SYSLOG_IDENTIFIER='systemd_demo',
)
handler = systemd.journal.JournalHandler(SYSLOG_IDENTIFIER='systemd_demo')
log.addHandler(handler)
log.debug('Hello systemd through logging framework')
log.removeHandler(handler)
handler.close()
# Cython bindings, by mosquito
import cysystemd.journal
cysystemd.journal.send(
message="Hello cysystemd",
priority=int(cysystemd.journal.Priority.DEBUG),
syslog_identifier='systemd_demo',
)
handler = cysystemd.journal.JournaldLogHandler(identifier='systemd_demo')
log.addHandler(handler)
log.debug('Hello cysystemd through logging framework')
log.removeHandler(handler)
handler.close()
@moreati
Copy link
Author

moreati commented Jun 15, 2021

:~/src$ journalctl --identifier=systemd_demo
-- Logs begin at Fri 2020-05-15 13:14:09 BST, end at Tue 2021-06-15 21:42:56 BST. --
Jun 15 21:41:31 martha systemd_demo[234175]: Hello systemd
Jun 15 21:41:31 martha systemd_demo[234175]: Hello systemd through logging framework
Jun 15 21:41:31 martha systemd_demo[234175]: Hello cysystemd
Jun 15 21:41:31 martha systemd_demo[234175]: Hello cysystemd through logging framework

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment