Per-model audit logs to TABLENAME_log with entry-point capture (HTTP/management/shell/celery)
Project description
Awesome Audit Log for Django
This is an awesome package to have your models logs in corresponding _log tables.
Having a single model/table as audit storage can cause heavy db operation and useless for large applications.
With this package you will have each model log in a separate table which can be beneficial if you want to truncate a specific model logs or run a query on them.
You can choose between having logs table in your default database or adding a new backend db as logs db.
Supported DBs to store logs:
- PostgreSQL
- MySQL
- SQLite
This package is in its early stage development and the following features will be added ASAP:
- Log rotation
- Mongo DB support
- Document page!
Compatible With
This package works on the below listed Django, Python versions and Databases.
- Django versions: 4.2, 5.0, 5.1
- Python versions: 3.10, 3.11, 3.12
- Databases: SQLite, PostgreSQL, MySQL
- Celery: Optional for Async logging
Installation
- Add App
INSTALLED_APPS = [
# ...
'awesome_audit_log.apps.AwesomeAuditLogConfig',
]
- Add Middleware
MIDDLEWARE = [
# ...
"awesome_audit_log.middleware.RequestEntryPointMiddleware",
]
- Settings
AWESOME_AUDIT_LOG = {
"ENABLED": True,
"DATABASE_ALIAS": "default",
# PostgreSQL schema for audit tables (defaults to 'public')
"PG_SCHEMA": None,
# Enable async logging with Celery (requires Celery to be installed and configured)
"ASYNC": False,
# "all" or list like ["app_label.ModelA", "app.ModelB"]
"AUDIT_MODELS": "all",
# like AUDIT_MODELS but for opt-out, useful when AUDIT_MODELS set to all
"NOT_AUDIT_MODELS": None,
"CAPTURE_HTTP": True,
# Capture context for management commands
"CAPTURE_COMMANDS": True,
# Capture context for Celery tasks
"CAPTURE_CELERY": True,
# set to False means if audit db is unavailable, silently skip logging (with a warning) instead of raising
"RAISE_ERROR_IF_DB_UNAVAILABLE": False,
# if audit alias missing/unavailable, use 'default' intentionally, this requires RAISE_ERROR_IF_DB_UNAVAILABLE is set to False
"FALLBACK_TO_DEFAULT": False,
}
Async Logging with Celery
This package supports async audit logging using Celery. When ASYNC is set to True, audit logs will be inserted asynchronously using Celery tasks, which can improve performance for high-traffic applications.
Setup
- Install Celery (if not already installed):
pip install celery
- Configure Celery in your Django project (this package doesn't configure Celery for you):
# settings.py
CELERY_BROKER_URL = 'redis://localhost:6379/0' # or your preferred broker
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
- Enable async logging:
AWESOME_AUDIT_LOG = {
"ASYNC": True, # Enable async logging
# ... other settings
}
Notes
- The package automatically detects if Celery is available and falls back to synchronous logging if not
- Works with any Celery broker (Redis, RabbitMQ, database, etc.)
- No additional configuration is required in this package - it uses your existing Celery setup
- Async logging is disabled by default for backward compatibility
- Important: Timestamps are captured at event time, not at database insert time, ensuring accurate audit logs even with async processing
Timestamp Accuracy
This package ensures that audit log timestamps (created_at) accurately reflect when events occur, not when they're saved to the database. This is especially important for async logging where there may be a delay between the event and database insertion.
See MIGRATION_GUIDE.md if you're upgrading from a version prior to 1.0.0.
Entry Point Detection
This package automatically captures audit context from different entry points in your application:
Supported Entry Points
- HTTP Requests: Automatically captured via middleware (when
CAPTURE_HTTPis enabled) - Management Commands: Automatically captured for all Django management commands (when
CAPTURE_COMMANDSis enabled) - Celery Tasks: Automatically captured for all Celery tasks and Celery Beat scheduled tasks (when
CAPTURE_CELERYis enabled)
Each audit log entry includes the entry_point field indicating the source:
http- HTTP requestsmanagement_command- Django management commandscelery_task- Celery tasks and Celery Beat scheduled tasks
Development
Preparation
# Install dependencies
poetry install
Running Tests and Linter Locally
# Run tests
poetry run pytest
# Run linting
poetry run ruff check awesome_audit_log tests
poetry run ruff format --check awesome_audit_log tests
Project details
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 awesome_audit_log_django-1.2.1.tar.gz.
File metadata
- Download URL: awesome_audit_log_django-1.2.1.tar.gz
- Upload date:
- Size: 13.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.12.12 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
28ae8f4b6337e707170825dcee6eb10339d01d5641e92c061f140cf103be6450
|
|
| MD5 |
a12eaaf2d9ed0cd0df5fbff4515a67e3
|
|
| BLAKE2b-256 |
8c6905fead1eff4e2358528b1b3e0ee7438af46b41f9bbab8085c357316496db
|
File details
Details for the file awesome_audit_log_django-1.2.1-py3-none-any.whl.
File metadata
- Download URL: awesome_audit_log_django-1.2.1-py3-none-any.whl
- Upload date:
- Size: 16.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.12.12 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac243006490a5f8064fa5be4999330b85ba19484676b44b79b88e13a0da5115a
|
|
| MD5 |
d71aa23de8560c24c54b3a0e61ba8d06
|
|
| BLAKE2b-256 |
cb70293f2f36eae2d19658d68c86647aab20cda9ef8e228acd83ee2d4c89d3de
|