From 8aef669d2b7fd261c70b3f8ac5001e1f98f1f46f Mon Sep 17 00:00:00 2001 From: Marvin Date: Thu, 2 Nov 2023 19:19:31 -0600 Subject: [PATCH] Use journald to log in python code --- reader/read.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/reader/read.py b/reader/read.py index 6e18e17..b4a8baa 100644 --- a/reader/read.py +++ b/reader/read.py @@ -3,14 +3,15 @@ import RPi.GPIO as GPIO from mfrc522 import SimpleMFRC522 from requests.exceptions import ConnectionError +import systemd.journal reader = SimpleMFRC522() while True: try: - print("Place an RFID tag near the reader...") + systemd.journal.send("Place an RFID tag near the reader...") id, text = reader.read() - print(f"Tag ID: {id}") + systemd.journal.send(f"Tag ID: {id}") # Define the URL to which you want to send the data url = 'http://yoda:3001/play' # Update the URL as needed @@ -24,18 +25,15 @@ response = requests.post(url, json=data, timeout=5, headers={'Content-Type': 'application/json'}) if response.status_code == 200: - print("Data sent successfully.") + systemd.journal.send("Data sent successfully.") break - # time.sleep(2) - # GPIO.cleanup() - # exit() # Exit the script on success else: - print("Failed to send data. Status code:", response.status_code) - print("Response body:", response.text) + systemd.journal.send(f"Failed to send data. Status code: {response.status_code}") + systemd.journal.send(f"Response body: {response.text}") break except ConnectionError as e: - print("Connection error:", e) + systemd.journal.send(f"Connection error: {e}") time.sleep(5) # Wait for a moment before retrying time.sleep(2)