From 1e21730bd41e4783f0fc259d37d7dee14e663cf4 Mon Sep 17 00:00:00 2001 From: Albert Date: Sun, 9 Nov 2025 20:00:54 +0100 Subject: [PATCH] Add logging: log vCard export errors to logs/app.log and console; update .gitignore --- .gitignore | 5 ++++- app.py | 20 +++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index c246afc..e256550 100644 --- a/.gitignore +++ b/.gitignore @@ -16,7 +16,10 @@ __pycache__/ .DS_Store # vCards -vcards +vcards/ + +# Logs +logs/ # anmeldung.sqbpro diff --git a/app.py b/app.py index 222ee72..4997ca7 100644 --- a/app.py +++ b/app.py @@ -3,6 +3,8 @@ from flask_sqlalchemy import SQLAlchemy import os import re from utils import generate_vcard +import logging +from logging.handlers import RotatingFileHandler BASE_DIR = os.path.dirname(os.path.abspath(__file__)) DB_PATH = os.path.join(BASE_DIR, 'anmeldung.db') @@ -13,6 +15,18 @@ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) +# --- logging setup ------------------------------------------------- +LOG_DIR = os.path.join(BASE_DIR, 'logs') +os.makedirs(LOG_DIR, exist_ok=True) +log_file = os.path.join(LOG_DIR, 'app.log') +handler = RotatingFileHandler(log_file, maxBytes=5 * 1024 * 1024, backupCount=3) +formatter = logging.Formatter('%(asctime)s %(levelname)s %(name)s: %(message)s') +handler.setFormatter(formatter) +handler.setLevel(logging.INFO) +app.logger.addHandler(handler) +app.logger.setLevel(logging.INFO) +# -------------------------------------------------------------------- + class Adresse(db.Model): id = db.Column(db.Integer, primary_key=True) @@ -100,9 +114,9 @@ def index(): # determine base dir: prefer app.config, then app attribute, then module BASE_DIR base_dir = app.config.get('BASE_DIR') if app.config.get('BASE_DIR') else getattr(app, 'BASE_DIR', BASE_DIR) generate_vcard(adresse, base_dir) - except Exception: - # nicht kritisch: bei Fehlern nicht die ganze Anfrage abbrechen - pass + except Exception as e: + # Log the exception with stack trace but don't abort the request + app.logger.exception('Fehler beim Erzeugen der vCard for adresse id=%s: %s', adresse.id if hasattr(adresse, 'id') else 'unknown', e) # Nach erfolgreichem Speichern weiterleiten return redirect(url_for('danke', id=adresse.id))