From f5c242d7c856e8f7be2cac8e8ca4dcb51d779515 Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Thu, 2 May 2024 07:32:38 +0200 Subject: [PATCH] write log since last boot to file --- cbpi/controller/system_controller.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/cbpi/controller/system_controller.py b/cbpi/controller/system_controller.py index 104ea71..ed13271 100644 --- a/cbpi/controller/system_controller.py +++ b/cbpi/controller/system_controller.py @@ -148,19 +148,25 @@ class SystemController: if logtime == "b": if systemd_available: #os.system('journalctl -b -u craftbeerpi.service --output cat > {}'.format(fullname)) - j = journal.Reader() - j.add_match(_TRANSPORT="kernel") + b = journal.Reader() + b.add_match(_TRANSPORT="kernel") result=[] - for entry in j: + for entry in b: message=entry['MESSAGE'] if message.find("Booting") != -1: result.append(entry['__REALTIME_TIMESTAMP']) + j = journal.Reader() j.add_match(_SYSTEMD_UNIT="craftbeerpi.service") j.seek_realtime(result[-1]) + result=[] for entry in j: - timestamp=entry['__REALTIME_TIMESTAMP'] - message=entry['MESSAGE'] - print(message) + result.append(entry['MESSAGE']) + try: + with open(fullname, 'w') as f: + for line in result: + f.write(f"{line}\n") + except Exception as e: + logging.error(e) else: if systemd_available: