Async MongoDB-backed audit log library with create, list, and pagination support.
Project description
library.audit-log
Lightweight async audit log library for MongoDB. It supports:
- create audit log records
- query audit log records as a list
- query audit log records with pagination
- custom MongoDB queries with automatic ISO datetime conversion
Installation
pip install library.audit-log
For local development inside this repository:
cd audit-log-lib
python -m build
Install development dependencies:
pip install -e .[dev]
Configuration
The library connects to MongoDB using a connection string.
Required config
connection_string: MongoDB connection string
Example:
MONGO_CONNECTION_STRING=mongodb://username:password@localhost:27017/audit_db?authSource=admin
Optional config
database_name: Explicit database name. If omitted, the library uses the database from the connection string.collection_name: MongoDB collection name. Default isaudit_trails.
Data model
Each audit log record supports these fields:
{
"owner_uid": "company-001",
"actor": {
"username": "alice",
"name": "Alice",
"role": "admin"
},
"entity": {
"uid": "order-123",
"name": "Order #123"
},
"entity_type": "order",
"action_type": "create"
}
The library automatically adds:
uidcreated_at
Basic usage
import os
from audit_log_lib import AuditLogCreate, AuditLogRepository
repository = AuditLogRepository(
connection_string=os.environ["MONGO_CONNECTION_STRING"],
database_name="audit_db",
collection_name="audit_trails",
)
async def create_log():
result = await repository.create(
obj_in=AuditLogCreate(
owner_uid="company-001",
actor={
"username": "alice",
"name": "Alice",
"role": "admin",
},
entity={
"uid": "order-123",
"name": "Order #123",
},
entity_type="order",
action_type="create",
)
)
return result
Get by uid
async def get_one(uid: str):
return await repository.get_by_uid(uid=uid)
Get as list
from datetime import datetime, timedelta
async def get_logs():
return await repository.get_by_owner(
owner_uid="company-001",
username="alice",
start_date=datetime.utcnow() - timedelta(days=7),
end_date=datetime.utcnow(),
search_keyword="order",
skip=0,
limit=50,
)
Get with pagination
If you want both items and total, use the paginated methods.
async def get_logs_paginated():
result = await repository.list_paginated_by_owner(
owner_uid="company-001",
entity_type="order",
action_type="create",
skip=0,
limit=20,
)
print(result.total)
print(result.items)
return result
Custom query with pagination
async def get_custom_logs():
return await repository.list_custom_paginated(
owner_uid="company-001",
custom_query={
"entity_type": "order",
"created_at": {
"$gte": "2024-01-01T00:00:00.000Z",
"$lte": "2024-12-31T23:59:59.000Z",
},
"$or": [
{"actor.username": "alice"},
{"entity.name": {"$regex": "Order", "$options": "i"}},
],
},
skip=0,
limit=20,
)
The library will automatically convert ISO datetime strings under $gte, $lte, $gt, and $lt into Python datetime objects before sending the query to MongoDB.
FastAPI example
import os
from fastapi import FastAPI
from audit_log_lib import AuditLogCreate, AuditLogRepository
app = FastAPI()
audit_repository = AuditLogRepository(
connection_string=os.environ["MONGO_CONNECTION_STRING"],
database_name="audit_db",
)
@app.post("/audit")
async def create_audit_log():
return await audit_repository.create(
obj_in=AuditLogCreate(
owner_uid="company-001",
actor={"username": "alice"},
entity={"uid": "item-001", "name": "Item 001"},
entity_type="item",
action_type="create",
)
)
See runnable examples in:
examples/basic_usage.pyexamples/fastapi_example.py
Tests
Run tests:
cd audit-log-lib
pytest
Main coverage included in tests/test_repository.py:
- create log
- get by uid
- get list by owner
- pagination with total/items
- custom query with ISO datetime conversion
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file library_audit_log-0.1.0.tar.gz.
File metadata
- Download URL: library_audit_log-0.1.0.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ddcd1c28492038dc27d7c8e40a97562f65b6df65d810f65d2e478a68a362bcc
|
|
| MD5 |
32301bb3650f0d877de1887a6da2f78e
|
|
| BLAKE2b-256 |
c7817bc7051a012009f1c3000d3c750905f07668b752333bd1e3a8e14fe34f18
|
File details
Details for the file library_audit_log-0.1.0-py3-none-any.whl.
File metadata
- Download URL: library_audit_log-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c950bea2a46aacbe0ca9cd8b81ca1d80f8736ed92f0b4eccf3aded36c45a86a6
|
|
| MD5 |
8fa3f059f6ac2729d89872ba9f9abc81
|
|
| BLAKE2b-256 |
e12ef0b5558133a6b649e9ce2cab0b9c30caa164902f64634d568e74a44ae9b7
|