26 lines
837 B
Python
26 lines
837 B
Python
"""Thin wrapper for writing audit log entries."""
|
|
import logging
|
|
from flask import request
|
|
|
|
log = logging.getLogger('app.audit')
|
|
|
|
|
|
def audit(action: str, description: str = ''):
|
|
"""
|
|
Write one audit log entry. Safe to call from any request context;
|
|
silently swallows DB errors so it never breaks the main flow.
|
|
"""
|
|
try:
|
|
from app.extensions import db
|
|
from app.models.audit_log import AuditLog
|
|
entry = AuditLog(
|
|
action=action,
|
|
description=description[:255] if description else '',
|
|
ip_address=request.remote_addr,
|
|
)
|
|
db.session.add(entry)
|
|
db.session.commit()
|
|
log.info('[audit] %s — %s (ip=%s)', action, description, request.remote_addr)
|
|
except Exception as exc:
|
|
log.warning('[audit] failed to write entry: %s', exc)
|