Add logging: log vCard export errors to logs/app.log and console; update .gitignore
This commit is contained in:
5
.gitignore
vendored
5
.gitignore
vendored
@@ -16,7 +16,10 @@ __pycache__/
|
||||
.DS_Store
|
||||
|
||||
# vCards
|
||||
vcards
|
||||
vcards/
|
||||
|
||||
# Logs
|
||||
logs/
|
||||
|
||||
#
|
||||
anmeldung.sqbpro
|
||||
|
||||
20
app.py
20
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))
|
||||
|
||||
Reference in New Issue
Block a user