Created
February 20, 2015 23:07
-
-
Save Jc2k/36c22e980ec750a7e081 to your computer and use it in GitHub Desktop.
Streaming log lines from journald in python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
from systemd import journal | |
class Journal(object): | |
def __init__(self, unit): | |
self.journal = journal.Reader() | |
# self.journal.log_level(journal.LOG_INFO) | |
self.journal.add_match(_SYSTEMD_UNIT=unit) | |
self.journal.seek_tail() | |
self.journal.get_previous() | |
def attach_to_loop(self, loop): | |
loop.add_reader(self.journal.fileno(), self.process) | |
def process_entry(self, entry): | |
print(str(entry['__REALTIME_TIMESTAMP'] )+ ' ' + entry['MESSAGE']) | |
def process(self): | |
if self.journal.process() != journal.APPEND: | |
return | |
for entry in self.journal: | |
self.process_entry(entry) | |
if __name__ == "__main__": | |
loop = asyncio.get_event_loop() | |
Journal("pubbot.service").attach_to_loop(loop) | |
loop.run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment