Skip to main content

A Django package for easy CRUD operation logging and container logs

Project description

Django Activity Audit

A Django package that extends the default logging mechanism to track CRUD operations and container logs.

Features

  • Automatic logging of CRUD operations (Create, Read, Update, Delete)
  • Tracks both HTTP requests and model changes
  • Custom log levels Audit(21) and API(22) for CRUD and Request-Response auditing.
  • Structured JSON logs for audit trails
  • Human-readable container logs
  • Separate log files for audit and container logs
  • Console and file output options
  • Async logging with QueueHandler for non-blocking performance

Installation

  1. Install the package:
pip install django-activity-audit
  1. Add 'django_activity_audit' to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
    ...
    'django_activity_audit',
]
  1. Add the middleware to your MIDDLEWARE in settings.py:
MIDDLEWARE = [
    ...
    'django_activity_audit.middleware.AuditLoggingMiddleware',
]
  1. Configure logging in settings.py:
from django_activity_audit import *

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "json": get_json_formatter(),
        "verbose": get_console_formatter(),
        "api_json": get_api_file_formatter(),
    },
    "handlers": {
        "console": {
            "level": "DEBUG",
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        },
        "file": get_json_handler(level="DEBUG", formatter="json"),
        "api_file": get_api_file_handler(formatter="api_json"),
    },
    "root": {"level": "DEBUG", "handlers": ["console", "file"]},
    "loggers": {
        "audit.request": {
            "handlers": ["api_file"],
            "level": "API",
            "propagate": False,
        },
        "django": {
            "handlers": ["console", "file"],
            "level": "INFO",
            "propagate": False,
        },
    }
}
  1. For external services logging, extend HTTPClient or SFTPClient
class ExternalService(HTTPClient):
    def __init__(self):
        super().__init__("service_name")

    def connect(self):
        url = "https://www.sample.com"
        response = self.get(url) # sample log structure below
  1. Create audit_logs folder in project directory

Async Logging Architecture

The package supports async logging using QueueHandler for better performance:

logger (audit.request) 
       │
       ▼
AsyncAuditLogHandler (QueueHandler)
       │  <-- pushes record
       ▼
log_queue
       │  <-- pulled by background thread
       ▼
QueueListener
       │
       ▼
AuditLogHandler (writes to file)

Benefits:

  • Non-blocking I/O operations
  • Better performance under high load
  • Log records are queued and processed in background threads
  • No impact on main application thread

Log Types

Container Logs

Console Log Format

'%(levelname)s %(asctime)s %(pathname)s %(module)s %(funcName)s %(message)s'
-----------------------------------------------------------------------------
INFO 2025-04-30 08:51:10,403 /app/patients/api/utils.py utils create_patient_with_contacts_and_diseases Patient 'd6c9a056-0b57-453a-8c0f-44319004b761 - Patient3' created.

File Log Format

{
    "timestamp": "2025-05-15 13:38:02.141",
    "level": "DEBUG",
    "name": "botocore.auth",
    "path": "/opt/venv/lib/python3.11/site-packages/botocore/auth.py",
    "module": "auth",
    "function": "add_auth",
    "message": "Calculating signature using v4 auth.",
    "exception": "",
    "request": "",
    "extra_fields": ""
}

CRUD Log

{
    "timestamp": "2025-08-16 17:06:32.403",
    "level": "AUDIT",
    "name": "audit.crud",
    "message": "CREATE event for User (id: 6f77b814-f9c1-4cab-a737-6677734bc303)",
    "model": "User",
    "event_type": "CREATE",
    "instance_id": "6f77b814-f9c1-4cab-a737-6677734bc303",
    "user": {
        "id": "cae8ffb4-ba52-409c-9a6f-e10362bfaf97",
        "title": "",
        "email": "example@source.com",
        "first_name": "",
        "middle_name": "",
        "last_name": "",
        "sex": "",
        "date_of_birth": null
    },
    "extra": {}
}

Request-Response Log

Incoming Log Format

{
    "timestamp": "2025-05-19 15:25:27.836",
    "level": "API",
    "name": "audit.request",
    "message": "Audit Internal Request",
    "service_name": "review_board",
    "request_type": "internal",
    "protocol": "http",
    "request_repr": {
        "method": "GET",
        "path": "/api/v1/health/",
        "query_params": {},
        "headers": {
            "Content-Type": "application/json",
        },
        "user": null,
        "body": {
            "title": "hello"
        }
    },
    "response_repr": {
        "status_code": 200,
        "headers": {
            "Content-Type": "application/json",
        },
        "body": {
            "status": "ok"
        }
    },
    "error_message": null,
    "execution_time": 5.376734018325806
}

External Log format

{
    "timestamp": "2025-05-19 15:25:27.717",
    "level": "API",
    "name": "audit.request",
    "message": "Audit External Service",
    "service_name": "apollo",
    "request_type": "external",
    "protocol": "http",
    "request_repr": "{'endpoint': 'https://www.sample.com', 'method': 'GET', 'headers': {}, 'body': {}}",
    "response_repr": "{'status_code': 200, 'body': {'title': 'title', 'expiresIn': 3600, 'error': None, 'errorDescription': None}}",
    "error_message": "",
    "execution_time": 5.16809344291687
}

Project Structure

    django_activity_audit/
        __init__.py
        apps.py
        constants.py
        logging.py
        middleware.py
        signals.py
        handlers.py
        utils.py
        tests.py
    setup.py
    README.md
    LICENSE
    MANIFEST.in

Notes

  • Compatible with Django 3.2+ and Python 3.7+.
  • Designed for easy integration with observability stacks using Vector, ClickHouse, and Grafana.
  • Capture Django CRUD operations automatically
  • Write structured JSON logs
  • Ready for production-grade logging pipelines
  • Simple pip install, reusable across projects
  • Zero additional database overhead!

Related Tools

  • Vector.dev <https://vector.dev/>_
  • ClickHouse <https://clickhouse.com/>_
  • Grafana <https://grafana.com/>_

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

django_activity_audit-1.0.3.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_activity_audit-1.0.3-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file django_activity_audit-1.0.3.tar.gz.

File metadata

  • Download URL: django_activity_audit-1.0.3.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for django_activity_audit-1.0.3.tar.gz
Algorithm Hash digest
SHA256 519568f4380e5ea0b1ba817fd2da70680905123457b8441a9ee5082d303d24d3
MD5 9ca99b4c91d86f466fbd06fb17945643
BLAKE2b-256 10fa5e291c66c53a40913332aae7aa3b2ae03c078d145bcdaed7f7d4d1941a26

See more details on using hashes here.

File details

Details for the file django_activity_audit-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_activity_audit-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7b435bbb2d4d2f0169b2f3ace44e72aa6f4164c19f5188eed4552b26eeea2015
MD5 0696e889319619122cd723c7e688c728
BLAKE2b-256 6df7f173267265899025c563416836bba279d348418cf342ee047c0fcff2f16b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page