Skip to main content

Universal Audit Framework (UAF) SDK

PyPI version Python 3.12+ License: MIT

A production-ready audit logging SDK for Python. Auto-captures WHO did WHAT, WHEN — with zero code in your endpoints.


Installation

# With FastAPI middleware + PostgreSQL (most common)
pip install universal-audit-logs[fastapi,postgres]

# Core only (REST transport)
pip install universal-audit-logs

# All transports
pip install universal-audit-logs[all]

FastAPI Middleware Setup (Recommended)

from fastapi import FastAPI
from starlette.middleware.sessions import SessionMiddleware
from audit_sdk.middleware.enhanced import (
    EnhancedAuditMiddleware,
    EnhancedAuditConfig,
)
from audit_sdk.transport.simple_database import SimpleAuditTransport

app = FastAPI()

# 1. Configure audit transport (writes to PostgreSQL)
transport = SimpleAuditTransport.for_postgres(
    host="localhost",
    port=5432,
    database="audit_db",
    username="your_user",
    password="your_password",
)

# 2. Add audit middleware
app.add_middleware(
    EnhancedAuditMiddleware,
    transport=transport,
    config=EnhancedAuditConfig(
        capture_request_body=True,
        excluded_paths=frozenset({"/health", "/docs", "/openapi.json"}),
    ),
)

# 3. Add session middleware (for user tracking)
app.add_middleware(SessionMiddleware, secret_key="your-secret-key")

Done. Every endpoint is now automatically audited. No audit.log() calls needed anywhere.


User Detection

The middleware auto-detects the logged-in user. Provide a UserProvider:

from audit_sdk.middleware.enhanced import EnhancedAuditMiddleware

class SessionUserProvider:
    async def get_user(self, request) -> dict | None:
        user = request.session.get("current_user")
        if user:
            return {
                "user_id": user["id"],
                "username": user["username"],
                "email": user.get("email", ""),
                "roles": user.get("roles", []),
            }
        return None

app.add_middleware(
    EnhancedAuditMiddleware,
    transport=transport,
    user_provider=SessionUserProvider(),
    config=EnhancedAuditConfig(capture_request_body=True),
)

What Gets Captured Automatically

Field Example
performed_by Sumanth
action CREATE, UPDATE, DELETE, LOGIN
action_message Sumanth created Employee John
functionality Employee Management
performed_on Employee #1
performed_at 2025-07-08 10:30:00+00
previous_state {"salary": 100000}
current_state {"salary": 120000}
changed_fields {"salary": {"old": 100000, "new": 120000}}

Transport Options

PostgreSQL (recommended)

from audit_sdk.transport.simple_database import SimpleAuditTransport

transport = SimpleAuditTransport.for_postgres(
    host="localhost", port=5432,
    database="audit_db", username="user", password="pass",
)

SQLite

transport = SimpleAuditTransport.for_sqlite(path="./audit.db")

MongoDB

pip install universal-audit-logs[mongodb]
from audit_sdk.transport.mongodb import MongoDBTransport

transport = MongoDBTransport(uri="mongodb://localhost:27017", database="audit_db")

REST (HTTP collector)

from audit_sdk import AuditClient, AuditSettings

settings = AuditSettings(endpoint="https://audit.example.com/api/v1/events")
audit = AuditClient(settings=settings)

Audit Log Table (auto-created)

CREATE TABLE audit_log (
    id              VARCHAR(36) PRIMARY KEY,
    performed_by    TEXT NOT NULL,
    action          VARCHAR(100) NOT NULL,
    action_message  TEXT,
    functionality   VARCHAR(200) NOT NULL,
    performed_on    TEXT NOT NULL,
    performed_at    TIMESTAMPTZ NOT NULL,
    previous_state  JSONB,
    current_state   JSONB,
    changed_fields  JSONB,
    http_context    JSONB
);

License

MIT - see LICENSE

Copyright (c) 2025 Sumanth

Release files for universal-audit-logs 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for universal-audit-logs 1.0.2
File Size Uploaded
universal_audit_logs-1.0.2.tar.gz 65.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for universal-audit-logs 1.0.2
File Interpreter ABI Platform
universal_audit_logs-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 150.0 kB

Release files / universal_audit_logs-1.0.2.tar.gz

Download URL universal_audit_logs-1.0.2.tar.gz
Size 65.3 kB
Tags Source
SHA-256 checksum
How to use checksums
2102a9772d5d10255e4bce7f6340f5937248c0559fa142aa777b87c6450c09ea
BLAKE2b-256 checksum
How to use checksums
136f6fe262aa13fe566098253f4e325fe7169a2e07ee668dceaf7837f8814ee7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Release files / universal_audit_logs-1.0.2-py3-none-any.whl

Download URL universal_audit_logs-1.0.2-py3-none-any.whl
Size 84.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c60a43eb9634c32c7945d5b457e5fd07d15c4e6d8fcf69b05f476a2eb9e918b5
BLAKE2b-256 checksum
How to use checksums
f1f6dbbd402f82ede5e3f6ba485ffb4c57a09d28850bd50120b861820b07d730
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page