auditlog-fastapi
Production-grade, reusable audit logging for FastAPI applications.
Features
- Multi-ORM Support: SQLAlchemy 2 (async), Tortoise ORM, SQLModel, Beanie (MongoDB), and raw asyncpg.
- Flexible Storage: PostgreSQL, MySQL, MariaDB, SQLite, and MongoDB.
- Explicit Configuration: Clean API with
AuditConfigandconfigure(). - Middleware Integration: Capture request/response data automatically.
- PII Masking: Redact sensitive fields from logs.
- Context Helpers: Enrich audit logs from route handlers.
- Lifespan Integration: Automatic startup/shutdown of database connections.
- Async & Non-blocking: Built with performance and safety in mind.
Installation
Using pip
# Basic install
pip install auditlog-fastapi
# With SQLAlchemy support (Postgres, MySQL, SQLite)
pip install "auditlog-fastapi[sqlalchemy]"
# With Tortoise ORM support
pip install "auditlog-fastapi[tortoise]"
# With SQLModel support
pip install "auditlog-fastapi[sqlmodel]"
# With MongoDB (Beanie) support
pip install "auditlog-fastapi[mongodb]"
# With raw asyncpg (PostgreSQL only) support
pip install "auditlog-fastapi[asyncpg]"
# Everything
pip install "auditlog-fastapi[all]"
Using Poetry
# Basic install
poetry add auditlog-fastapi
# With specific extras
poetry add "auditlog-fastapi[sqlalchemy]"
poetry add "auditlog-fastapi[mongodb]"
Quick Start (SQLAlchemy + SQLite)
from fastapi import FastAPI
from auditlog_fastapi import AuditMiddleware, AuditConfig, create_audit_lifespan
# 1. Configure the audit log
config = AuditConfig(
orm="sqlalchemy",
dsn="sqlite+aiosqlite:///./audit.db",
table_name="audit_logs",
auto_create_table=True,
)
# 2. Create lifespan handler
app = FastAPI(lifespan=create_audit_lifespan(config))
# 3. Add middleware
app.add_middleware(
AuditMiddleware,
log_request_body=True
)
@app.get("/")
async def root():
return {"message": "Hello World"}
User Configuration Guide
SQLAlchemy + PostgreSQL
config = AuditConfig(
orm="sqlalchemy",
dsn="postgresql+asyncpg://user:pass@localhost:5432/mydb",
table_name="audit_logs",
auto_create_table=True,
sqlalchemy_pool_size=10,
mask_fields=["password", "token"],
)
SQLAlchemy + MySQL
config = AuditConfig(
orm="sqlalchemy",
dsn="mysql+aiomysql://user:pass@localhost:3306/mydb",
auto_create_table=True,
)
Tortoise ORM + PostgreSQL
config = AuditConfig(
orm="tortoise",
dsn="postgres://user:pass@localhost:5432/mydb",
auto_create_table=True,
)
MongoDB via Beanie
config = AuditConfig(
orm="beanie",
dsn="mongodb://localhost:27017",
mongodb_database="myapp",
table_name="audit_logs", # becomes collection name
)
Raw asyncpg (PostgreSQL, maximum performance)
config = AuditConfig(
orm="asyncpg",
dsn="postgresql://user:pass@localhost:5432/mydb",
batch_size=200,
)
Using with Alembic (SQLAlchemy only)
# In alembic/env.py — include audit table in your migrations
from auditlog_fastapi.db.sqlalchemy_table import AuditBase
target_metadata = [YourBase.metadata, AuditBase.metadata]
Enriching Logs from Routes
from auditlog_fastapi import set_audit_action, set_audit_resource, set_audit_extra
@app.post("/items")
async def create_item(item_id: str):
set_audit_action("item.create")
set_audit_resource("item", item_id)
set_audit_extra("metadata", {"source": "admin_panel"})
return {"status": "ok"}
Retrieving Audit Logs
auditlog-fastapi provides a built-in helper to add a route for querying and filtering your audit logs.
from fastapi import FastAPI
from auditlog_fastapi import add_audit_log_routes
app = FastAPI(...)
# Register the GET /audit-logs route
add_audit_log_routes(
app,
path="/audit-logs", # default
tags=["Audit Logs"] # optional tags for OpenAPI
)
Filtering and Pagination
The added route supports several query parameters:
- Pagination:
limit(default 100, max 1000) andoffset(default 0). - Filters:
method,path,status_code,user_id, andaction.
Example request:
GET /audit-logs?method=POST&status_code=201&limit=20
Configuration Reference (AuditConfig)
| Parameter | Type | Default | Description |
|---|---|---|---|
orm |
str |
Required | One of: sqlalchemy, tortoise, sqlmodel, beanie, asyncpg. |
dsn |
str |
Required | Connection string for the database. |
table_name |
str |
"audit_logs" |
Name of the table or collection. |
auto_create_table |
bool |
True |
Whether to create the table on startup. |
batch_size |
int |
1 |
Set > 1 to enable batching (not all backends yet). |
mask_fields |
list[str] |
[] |
PII fields to mask in request bodies. |
on_storage_error |
Callable |
None |
Optional callback for storage errors. |
Development and Examples
To run the examples that require a real database (PostgreSQL, MongoDB, MySQL), you can use the provided Docker Compose file:
docker-compose up -d
This will start:
- PostgreSQL at
localhost:5432(user:user, pass:pass, db:audit_db) - MongoDB at
localhost:27017 - MySQL at
localhost:3306(user:user, pass:pass, db:audit_db)
Running the MongoDB Example
poetry run python examples/beanie_mongodb_usage.py
Running the Asyncpg Example
poetry run python examples/asyncpg_usage.py
Future Features
- Admin UI: Build a simple web UI for viewing/searching audit logs.
- Custom Storage Backends: Allow users to plug in custom storage backends (e.g., S3, Redis, external APIs).
- Event Hooks: Add hooks for pre/post log processing (e.g., for enrichment, notifications).
- Log Export: Support exporting logs to CSV, JSON, or external log management systems.
- Retention Policies: Add configurable log retention and automatic cleanup.
- Multi-Tenancy: Support tenant-aware logging for SaaS apps.
- Security: Encrypt sensitive log fields at rest, and add role-based access for log viewing.
- CLI Tooling: Provide CLI commands for log inspection, export, and management.
- OpenTelemetry Integration: Integrate with OpenTelemetry for distributed tracing and correlation.
- Documentation: Expand usage examples and add troubleshooting guides.
License
MIT
Metadata
Release files for auditlog-fastapi 0.3.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| auditlog_fastapi-0.3.3.tar.gz | 18.0 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| auditlog_fastapi-0.3.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 43.2 kB
Release files / auditlog_fastapi-0.3.3.tar.gz
| Download URL | auditlog_fastapi-0.3.3.tar.gz |
|---|---|
| Size | 18.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f77919d9e582e4f14aece47943998744917bff87390e5e11407c0dc4d57903eb
|
|
BLAKE2b-256 checksum How to use checksums |
44d9e1e40f74f679e62b8c0fc8ae45e44c8aadad0d3a8d069924c1e5765b9d5a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 26, 2026.
Transparency logRelease files / auditlog_fastapi-0.3.3-py3-none-any.whl
| Download URL | auditlog_fastapi-0.3.3-py3-none-any.whl |
|---|---|
| Size | 25.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
42e569bb23bb8d40fd5dd4ec1500307c1247808c15e2b4dedacc5087fa877722
|
|
BLAKE2b-256 checksum How to use checksums |
146b9522e193c1550586a9a13d7a2721ab28a075f7f80f27796f52c0a56e037a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 26, 2026.
Transparency logSecurity for auditlog-fastapi 0.3.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for auditlog_fastapi-0.3.3.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 26, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.