journaliser/journaliser.py
2025-07-30 22:33:33 +02:00

45 lines
1.4 KiB
Python

import logging
import report
import subprocess
logger = logging.getLogger("root")
report.add_handler_socket(logger)
class mkrecord(object):
def __init__(self, line):
self.line = line.strip("\n")
self.module = line.split(" ")[4].split("[")[0]
if __name__ == "__main__":
command = ['journalctl', '-fn', '500']
# Start the process using Popen
try:
with subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True, # Decode stdout/stderr as text using default encoding
bufsize=1 # Use line-buffering
) as process:
print(f"--- Following journal output for: {' '.join(command)} ---")
print("--- Press Ctrl+C to exit ---")
# Read from stdout line by line as it's generated
for line in process.stdout:
record = mkrecord(line)
l = logger.getChild(record.module)
if "crit" in record.line.lower():
l.critical(record.line)
elif "error" in record.line.lower():
l.error(record.line)
elif "warn" in record.line.lower():
l.warning(record.line)
elif "info" in record.line.lower():
l.info(record.line)
else:
l.debug(record.line)
except KeyboardInterrupt:
print(" Bye...")