From 551a4abd1396c19c9f0d37822bd557e281a0d7b8 Mon Sep 17 00:00:00 2001 From: hubobel Date: Tue, 16 Jun 2026 17:32:31 +0200 Subject: [PATCH] Logging --- categorize_transactions.py | 68 ++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/categorize_transactions.py b/categorize_transactions.py index bded8a1..4f780bd 100644 --- a/categorize_transactions.py +++ b/categorize_transactions.py @@ -10,6 +10,24 @@ from openpyxl import Workbook, load_workbook import subprocess from shutil import which, copy2 import platform +import logging + +# -------------------------------------------------- +# Logging +# -------------------------------------------------- + +log_dir = Path.home() / "logs" +log_dir.mkdir(exist_ok=True) + +log_file = log_dir / "categorize_transactions.log" + +logging.basicConfig( + filename=log_file, + level=logging.INFO, + format="%(asctime)s [%(levelname)s] %(message)s", +) + +logging.info("=== Scriptstart ===") # -------------------------------------------------- # Kalenderwoche bestimmen @@ -85,7 +103,9 @@ excel_file = ( # -------------------------------------------------- if not source_json.exists(): - print(f"Datei nicht gefunden: {source_json}") + logging.error( + f"Transaktionsdatei nicht gefunden: {source_json}" + ) sys.exit(1) # -------------------------------------------------- @@ -339,28 +359,28 @@ with open(summary_file, "w", encoding="utf-8") as outfile: # Ausgabe # -------------------------------------------------- -print(f"Jahr : {year}") -print(f"Kalenderwoche : {week}") -print(f"Transaktionen : {len(categorized_transactions)}") -print() +logging.info(f"Jahr : {year}") +logging.info(f"Kalenderwoche : {week}") +logging.info( + f"Transaktionen : {len(categorized_transactions)}" +) -print("Summen pro Kategorie:") -print() +logging.info("----------------------------------------") +logging.info("Summen pro Kategorie") +logging.info("----------------------------------------") for category, values in sorted(category_totals.items()): - - print( - f"{category:20} " - f"Einnahmen: {values['income']:10.2f} EUR " - f"Ausgaben: {values['expenses']:10.2f} EUR " - f"Saldo: {values['saldo']:10.2f} EUR" + logging.info( + f"Kategorie={category}; " + f"Einnahmen={values['income']:.2f}; " + f"Ausgaben={values['expenses']:.2f}; " + f"Saldo={values['saldo']:.2f}" ) -print() -print(f"CSV : {csv_file}") -print(f"JSON : {json_file}") -print(f"Summary : {summary_file}") -print(f"Excel : {excel_file}") +logging.info(f"CSV : {csv_file}") +logging.info(f"JSON : {json_file}") +logging.info(f"Summary : {summary_file}") +logging.info(f"Excel : {excel_file}") # -------------------------------------------------- # Synchronisation @@ -380,11 +400,11 @@ if which("rclone"): check=True ) - print("Excel nach Nextcloud synchronisiert.") + logging.info("Excel nach Nextcloud synchronisiert.") except Exception as e: - print(f"rclone Fehler: {e}") + logging.error(f"rclone Fehler: {e}") elif platform.system() == "Windows": @@ -412,17 +432,17 @@ elif platform.system() == "Windows": target_file ) - print( - f"Excel nach {target_file} kopiert." + logging.info( + f"Nextcloud-Synchronisation erfolgreich: {target_file}" ) except Exception as e: - print( + logging.error( f"Nextcloud-Kopie fehlgeschlagen: {e}" ) else: - print( + logging.warning( "Keine Synchronisationsmethode gefunden." ) \ No newline at end of file