Files
FabledScribe/alembic/versions/0010_add_app_logs_table.py
T
bvandeusen d874e0e2ae Add application logging, SMTP email notifications, and supporting changes
Phase 5.4 — Application Logging:
- AppLog model + migration 0010 for unified audit/usage/error logging
- Usage logging middleware in app.py (after_request for /api/* requests)
- Error logging in 500 handler with traceback capture
- Audit logging for auth events (register, login, login_failed, logout,
  password_change) and admin actions (backup, restore, user_delete,
  registration_toggle, smtp_config, smtp_test)
- Admin log viewer (LogsView.vue) with stats, category/search/date
  filters, paginated table with expandable detail rows
- Admin logs API endpoints in admin.py (GET /logs, GET /logs/stats)
- Configurable retention via LOG_RETENTION_DAYS with hourly cleanup

Phase 5.5 — SMTP Email Notifications:
- aiosmtplib dependency for async email sending
- Email service (services/email.py) with STARTTLS/implicit TLS support
- Notification service (services/notifications.py) for security alerts
  and task due date reminders with per-user preferences
- Admin SMTP config endpoints (GET/PUT /api/admin/smtp, POST test)
- SMTP config in Config class with env var + Docker secret support
- Settings UI: notification preferences for all users, SMTP config
  section for admin with test email

Other changes:
- stream_chat() now accepts optional options dict (for num_predict)
- Increase assist MAX_BODY_CHARS from 3000 to 8000
- get_user_by_username() added to auth service
- apiStreamPost buffer processing refactored for robustness
- AppHeader: admin Logs nav link
- Router: /admin/logs route

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-13 08:34:52 -05:00

41 lines
1.4 KiB
Python

"""Add app_logs table for audit, usage, and error logging."""
from alembic import op
revision = "0010"
down_revision = "0009"
def upgrade() -> None:
op.execute("""
CREATE TABLE IF NOT EXISTS app_logs (
id SERIAL PRIMARY KEY,
category TEXT NOT NULL,
user_id INTEGER REFERENCES users(id) ON DELETE SET NULL,
username TEXT,
action TEXT,
endpoint TEXT,
method TEXT,
status_code INTEGER,
duration_ms REAL,
ip_address TEXT,
details TEXT,
created_at TIMESTAMPTZ DEFAULT now()
)
""")
op.execute("CREATE INDEX IF NOT EXISTS ix_app_logs_category ON app_logs (category)")
op.execute("CREATE INDEX IF NOT EXISTS ix_app_logs_user_id ON app_logs (user_id)")
op.execute("CREATE INDEX IF NOT EXISTS ix_app_logs_created_at ON app_logs (created_at)")
op.execute(
"CREATE INDEX IF NOT EXISTS ix_app_logs_category_created_at "
"ON app_logs (category, created_at DESC)"
)
def downgrade() -> None:
op.execute("DROP INDEX IF EXISTS ix_app_logs_category_created_at")
op.execute("DROP INDEX IF EXISTS ix_app_logs_created_at")
op.execute("DROP INDEX IF EXISTS ix_app_logs_user_id")
op.execute("DROP INDEX IF EXISTS ix_app_logs_category")
op.execute("DROP TABLE IF EXISTS app_logs")