Skip to content

Instantly share code, notes, and snippets.

@falconindy
Last active November 10, 2021 23:04
Show Gist options
  • Save falconindy/c7f188a670b5e70d5ba5 to your computer and use it in GitHub Desktop.
Save falconindy/c7f188a670b5e70d5ba5 to your computer and use it in GitHub Desktop.
Journal printer with custom formatting
#!/usr/bin/env python
'''
A journal reader which offers custom message formatting on output.
Example:
./journalprintf \
'{__REALTIME_TIMESTAMP} {_HOSTNAME} {_SYSTEMD_UNIT} {_COMM}[{_PID}]: {MESSAGE}' \
_SYSTEMD_UNIT=dnsmasq.service \
_SYSTEMD_UNIT=systemd-networkd.service
Known limitations:
If a field is requested field is missing from the journal entry, the field
will be skipped entirely, rather than being replaced with something/nothing.
'''
import sys
from systemd import journal
with journal.Reader() as j:
j.this_boot()
for m in sys.argv[2:]:
j.add_match(m)
for entry in j:
try:
print(sys.argv[1].format(**entry))
except KeyError as e:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment