Add logging: log vCard export errors to logs/app.log and console; update .gitignore

This commit is contained in:
Albert
2025-11-09 20:00:54 +01:00
parent 62613a2f39
commit 1e21730bd4
2 changed files with 21 additions and 4 deletions

5
.gitignore vendored
View File

@@ -16,7 +16,10 @@ __pycache__/
.DS_Store .DS_Store
# vCards # vCards
vcards vcards/
# Logs
logs/
# #
anmeldung.sqbpro anmeldung.sqbpro

20
app.py
View File

@@ -3,6 +3,8 @@ from flask_sqlalchemy import SQLAlchemy
import os import os
import re import re
from utils import generate_vcard from utils import generate_vcard
import logging
from logging.handlers import RotatingFileHandler
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DB_PATH = os.path.join(BASE_DIR, 'anmeldung.db') DB_PATH = os.path.join(BASE_DIR, 'anmeldung.db')
@@ -13,6 +15,18 @@ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app) 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): class Adresse(db.Model):
id = db.Column(db.Integer, primary_key=True) 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 # 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) 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) generate_vcard(adresse, base_dir)
except Exception: except Exception as e:
# nicht kritisch: bei Fehlern nicht die ganze Anfrage abbrechen # Log the exception with stack trace but don't abort the request
pass 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 # Nach erfolgreichem Speichern weiterleiten
return redirect(url_for('danke', id=adresse.id)) return redirect(url_for('danke', id=adresse.id))