Skip to main content

Easy Audit Logging

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

Installation

  1. Install the package:
pip install easy-audit-logging
  1. Add 'easy_audit_logging' to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
    ...
    'easy_audit_logging',
]
  1. Add the middleware to your MIDDLEWARE in settings.py:
MIDDLEWARE = [
    ...
    'easy_audit_logging.middleware.EasyLoggingMiddleware',
]
  1. Configure logging in settings.py:
from easy_audit_logging 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

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": ""
}

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
}

License

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

Metadata

Release files for easy-audit-logging 0.1.31

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

Source distribution (sdist)

Source distribution for easy-audit-logging 0.1.31
File Size Uploaded
easy_audit_logging-0.1.31.tar.gz 12.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for easy-audit-logging 0.1.31
File Interpreter ABI Platform
easy_audit_logging-0.1.31-py3-none-any.whl Python 3 none any Details

Total release size: 27.6 kB

Release files / easy_audit_logging-0.1.31.tar.gz

Download URL easy_audit_logging-0.1.31.tar.gz
Size 12.9 kB
Tags Source
SHA-256 checksum
How to use checksums
d94a703679e8b16eae344fdce99fd60f57f3f956baade0c4ab2d5de256ee63f7
BLAKE2b-256 checksum
How to use checksums
66cd6ad5c74de17cb38cef1ef82609d6483a0aa2e93314e9a5372c137c2578f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release files / easy_audit_logging-0.1.31-py3-none-any.whl

Download URL easy_audit_logging-0.1.31-py3-none-any.whl
Size 14.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fc01fd8368020c423754dc42d0e99e3183e28cde5c439b22d24defd689343356
BLAKE2b-256 checksum
How to use checksums
66ee7888aa8e160478348ad6ccbe6450d30ff8ef835ee87cabe909cd2c35865d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release history Release notifications | RSS feed

This release

0.1.31 This release

2 release files

0.1.30

2 release files

0.1.29

2 release files

0.1.28

2 release files

0.1.27

2 release files

0.1.26

2 release files

0.1.25

2 release files

0.1.24

2 release files

0.1.23

2 release files

0.1.22

2 release files

0.1.21

2 release files

0.1.20

2 release files

0.1.19

2 release files

0.1.18

2 release files

0.1.17

2 release files

0.1.16

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.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