46 lines
1.5 KiB
Python
46 lines
1.5 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)
|
|
lower_line = record.line.lower()
|
|
if "crit" in lower_line or "exception" in lower_line:
|
|
l.critical(record.line)
|
|
elif "error" in lower_line:
|
|
l.error(record.line)
|
|
elif "warn" in lower_line:
|
|
l.warning(record.line)
|
|
elif "info" in lower_line:
|
|
l.info(record.line)
|
|
else:
|
|
l.debug(record.line)
|
|
except KeyboardInterrupt:
|
|
print(" Bye...")
|