django-activity-audit


Namedjango-activity-audit JSON
Version 1.0.0 PyPI version JSON
download
home_pageNone
SummaryA Django package for easy CRUD operation logging and container logs
upload_time2025-08-18 06:29:23
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT
keywords audit django logging utilities
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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

## Installation

1. Install the package:
```bash
pip install django-activity-audit
```

2. Add 'django_activity_audit' to your INSTALLED_APPS in settings.py:
```python
INSTALLED_APPS = [
    ...
    'django_activity_audit',
]
```

3. Add the middleware to your MIDDLEWARE in settings.py:
```python
MIDDLEWARE = [
    ...
    'django_activity_audit.middleware.AuditLoggingMiddleware',
]
```

4. Configure logging in settings.py:
```python
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,
        },
    }
}
```

5. For external services logging, extend ```HTTPClient or SFTPClient```
```python
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
```

7. Create ```audit_logs``` folder in project directory

## Log Types

### Container Logs
#### Console Log Format
```shell
'%(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
```json
{
    "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
```json
{
    "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
```json
{
    "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
```json
{
    "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
```text
    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.




            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "django-activity-audit",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "audit, django, logging, utilities",
    "author": null,
    "author_email": "Shreeshan <shreeshan256@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/9b/2f/62bb8fca055e585c311571ac15b19f98280ce216c231fd356b6a99a76cba/django_activity_audit-1.0.0.tar.gz",
    "platform": null,
    "description": "# Django Activity Audit\n\nA Django package that extends the default logging mechanism to track CRUD operations and container logs.\n\n## Features\n\n- Automatic logging of CRUD operations (Create, Read, Update, Delete)\n- Tracks both HTTP requests and model changes\n- Custom log levels Audit(21) and API(22) for CRUD and Request-Response auditing.\n- Structured JSON logs for audit trails\n- Human-readable container logs\n- Separate log files for audit and container logs\n- Console and file output options\n\n## Installation\n\n1. Install the package:\n```bash\npip install django-activity-audit\n```\n\n2. Add 'django_activity_audit' to your INSTALLED_APPS in settings.py:\n```python\nINSTALLED_APPS = [\n    ...\n    'django_activity_audit',\n]\n```\n\n3. Add the middleware to your MIDDLEWARE in settings.py:\n```python\nMIDDLEWARE = [\n    ...\n    'django_activity_audit.middleware.AuditLoggingMiddleware',\n]\n```\n\n4. Configure logging in settings.py:\n```python\nfrom django_activity_audit import *\n\nLOGGING = {\n    \"version\": 1,\n    \"disable_existing_loggers\": False,\n    \"formatters\": {\n        \"json\": get_json_formatter(),\n        \"verbose\": get_console_formatter(),\n        \"api_json\": get_api_file_formatter(),\n    },\n    \"handlers\": {\n        \"console\": {\n            \"level\": \"DEBUG\",\n            \"class\": \"logging.StreamHandler\",\n            \"formatter\": \"verbose\",\n        },\n        \"file\": get_json_handler(level=\"DEBUG\", formatter=\"json\"),\n        \"api_file\": get_api_file_handler(formatter=\"api_json\"),\n    },\n    \"root\": {\"level\": \"DEBUG\", \"handlers\": [\"console\", \"file\"]},\n    \"loggers\": {\n        \"audit.request\": {\n            \"handlers\": [\"api_file\"],\n            \"level\": \"API\",\n            \"propagate\": False,\n        },\n        \"django\": {\n            \"handlers\": [\"console\", \"file\"],\n            \"level\": \"INFO\",\n            \"propagate\": False,\n        },\n    }\n}\n```\n\n5. For external services logging, extend ```HTTPClient or SFTPClient```\n```python\nclass ExternalService(HTTPClient):\n    def __init__(self):\n        super().__init__(\"service_name\")\n\n    def connect(self):\n        url = \"https://www.sample.com\"\n        response = self.get(url) # sample log structure below\n```\n\n7. Create ```audit_logs``` folder in project directory\n\n## Log Types\n\n### Container Logs\n#### Console Log Format\n```shell\n'%(levelname)s %(asctime)s %(pathname)s %(module)s %(funcName)s %(message)s'\n-----------------------------------------------------------------------------\nINFO 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.\n```\n\n#### File Log Format\n```json\n{\n    \"timestamp\": \"2025-05-15 13:38:02.141\",\n    \"level\": \"DEBUG\",\n    \"name\": \"botocore.auth\",\n    \"path\": \"/opt/venv/lib/python3.11/site-packages/botocore/auth.py\",\n    \"module\": \"auth\",\n    \"function\": \"add_auth\",\n    \"message\": \"Calculating signature using v4 auth.\",\n    \"exception\": \"\",\n    \"request\": \"\",\n    \"extra_fields\": \"\"\n}\n```\n\n### CRUD Log\n```json\n{\n    \"timestamp\": \"2025-08-16 17:06:32.403\",\n    \"level\": \"AUDIT\",\n    \"name\": \"audit.crud\",\n    \"message\": \"CREATE event for User (id: 6f77b814-f9c1-4cab-a737-6677734bc303)\",\n    \"model\": \"User\",\n    \"event_type\": \"CREATE\",\n    \"instance_id\": \"6f77b814-f9c1-4cab-a737-6677734bc303\",\n    \"user\": {\n        \"id\": \"cae8ffb4-ba52-409c-9a6f-e10362bfaf97\",\n        \"title\": \"\",\n        \"email\": \"example@source.com\",\n        \"first_name\": \"\",\n        \"middle_name\": \"\",\n        \"last_name\": \"\",\n        \"sex\": \"\",\n        \"date_of_birth\": null\n    },\n    \"extra\": {}\n}\n```\n\n### Request-Response Log\n#### Incoming Log Format\n```json\n{\n    \"timestamp\": \"2025-05-19 15:25:27.836\",\n    \"level\": \"API\",\n    \"name\": \"audit.request\",\n    \"message\": \"Audit Internal Request\",\n    \"service_name\": \"review_board\",\n    \"request_type\": \"internal\",\n    \"protocol\": \"http\",\n    \"request_repr\": {\n        \"method\": \"GET\",\n        \"path\": \"/api/v1/health/\",\n        \"query_params\": {},\n        \"headers\": {\n            \"Content-Type\": \"application/json\",\n        },\n        \"user\": null,\n        \"body\": {\n            \"title\": \"hello\"\n        }\n    },\n    \"response_repr\": {\n        \"status_code\": 200,\n        \"headers\": {\n            \"Content-Type\": \"application/json\",\n        },\n        \"body\": {\n            \"status\": \"ok\"\n        }\n    },\n    \"error_message\": null,\n    \"execution_time\": 5.376734018325806\n}\n```\n\n#### External Log format\n```json\n{\n    \"timestamp\": \"2025-05-19 15:25:27.717\",\n    \"level\": \"API\",\n    \"name\": \"audit.request\",\n    \"message\": \"Audit External Service\",\n    \"service_name\": \"apollo\",\n    \"request_type\": \"external\",\n    \"protocol\": \"http\",\n    \"request_repr\": \"{'endpoint': 'https://www.sample.com', 'method': 'GET', 'headers': {}, 'body': {}}\",\n    \"response_repr\": \"{'status_code': 200, 'body': {'title': 'title', 'expiresIn': 3600, 'error': None, 'errorDescription': None}}\",\n    \"error_message\": \"\",\n    \"execution_time\": 5.16809344291687\n}\n```\n## Project Structure\n```text\n    django_activity_audit/\n        __init__.py\n        apps.py\n        constants.py\n        logging.py\n        middleware.py\n        signals.py\n        handlers.py\n        utils.py\n        tests.py\n    setup.py\n    README.md\n    LICENSE\n    MANIFEST.in\n```\n\n## Notes\n\n- Compatible with **Django 3.2+** and **Python 3.7+**.\n- Designed for easy integration with observability stacks using Vector, ClickHouse, and Grafana.\n- Capture Django CRUD operations automatically\n- Write structured JSON logs\n- Ready for production-grade logging pipelines\n- Simple pip install, reusable across projects\n- Zero additional database overhead! \n\n## Related Tools\n\n- `Vector.dev <https://vector.dev/>`_\n- `ClickHouse <https://clickhouse.com/>`_ \n- `Grafana <https://grafana.com/>`_\n\n## License\n\nThis project is licensed under the MIT License - see the LICENSE file for details.\n\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A Django package for easy CRUD operation logging and container logs",
    "version": "1.0.0",
    "project_urls": {
        "Documentation": "https://github.com/shree256/django-activity-audit",
        "Homepage": "https://github.com/shree256/django-activity-audit",
        "Repository": "https://github.com/shree256/django-activity-audit"
    },
    "split_keywords": [
        "audit",
        " django",
        " logging",
        " utilities"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "32f517a8f639e2342c39628b1db2a2d98294e97a2b62fa5c3ed56d8b2113582b",
                "md5": "42e7b59304cb7f3535876f134749fe95",
                "sha256": "6924b1415f98dc17d8ab824022e5eed6a883bb75eebe3dc80cfeed117d9fb232"
            },
            "downloads": -1,
            "filename": "django_activity_audit-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "42e7b59304cb7f3535876f134749fe95",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 15328,
            "upload_time": "2025-08-18T06:29:24",
            "upload_time_iso_8601": "2025-08-18T06:29:24.897438Z",
            "url": "https://files.pythonhosted.org/packages/32/f5/17a8f639e2342c39628b1db2a2d98294e97a2b62fa5c3ed56d8b2113582b/django_activity_audit-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "9b2f62bb8fca055e585c311571ac15b19f98280ce216c231fd356b6a99a76cba",
                "md5": "8fbe59375b8bf3ed03773ef85779670c",
                "sha256": "51f89def9c09a6c2c5aa987e25b42671c11c97fa9b0b1fc8d47b4e5de1f38515"
            },
            "downloads": -1,
            "filename": "django_activity_audit-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "8fbe59375b8bf3ed03773ef85779670c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 13581,
            "upload_time": "2025-08-18T06:29:23",
            "upload_time_iso_8601": "2025-08-18T06:29:23.445494Z",
            "url": "https://files.pythonhosted.org/packages/9b/2f/62bb8fca055e585c311571ac15b19f98280ce216c231fd356b6a99a76cba/django_activity_audit-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-18 06:29:23",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "shree256",
    "github_project": "django-activity-audit",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "django-activity-audit"
}
        
Elapsed time: 1.38737s