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 sumanth-audit-sdk[fastapi,postgres]

# Core only (REST transport)
pip install sumanth-audit-sdk

# All transports
pip install sumanth-audit-sdk[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 sumanth-audit-sdk[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 sumanth-audit-sdk 1.0.1

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

Source distribution (sdist)

Source distribution for sumanth-audit-sdk 1.0.1
File Size Uploaded
sumanth_audit_sdk-1.0.1.tar.gz 65.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sumanth-audit-sdk 1.0.1
File Interpreter ABI Platform
sumanth_audit_sdk-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 150.0 kB

Release files / sumanth_audit_sdk-1.0.1.tar.gz

Download URL sumanth_audit_sdk-1.0.1.tar.gz
Size 65.3 kB
Tags Source
SHA-256 checksum
How to use checksums
bcc26a69e2d82ab5d36a40c041bf93bcade5820b4f82cb9e8964ba189210f144
BLAKE2b-256 checksum
How to use checksums
c18d921f9d6e90b63697f9c3e97150f29ec4597f3e253a29a78b15ee2f238e4a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Release files / sumanth_audit_sdk-1.0.1-py3-none-any.whl

Download URL sumanth_audit_sdk-1.0.1-py3-none-any.whl
Size 84.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d58051259de1b0e7517021b4161345c106bfba2afc3fbca9127365580ca1000c
BLAKE2b-256 checksum
How to use checksums
6941f9ad3036780a9922bb82b017ba6e45d92fb5a872ac00d89c00305ff4bce7
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.1 This release

2 release files

1.0.0

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