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
|
.DS_Store
|
||||||
|
|
||||||
# vCards
|
# vCards
|
||||||
vcards
|
vcards/
|
||||||
|
|
||||||
|
# Logs
|
||||||
|
logs/
|
||||||
|
|
||||||
#
|
#
|
||||||
anmeldung.sqbpro
|
anmeldung.sqbpro
|
||||||
|
|||||||
20
app.py
20
app.py
@@ -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))
|
||||||
|
|||||||
Reference in New Issue
Block a user