django-activity-audit


Namedjango-activity-audit JSON
Version 1.2.6 PyPI version JSON
download
home_pageNone
SummaryA Django package for easy CRUD operation logging and container logs
upload_time2025-10-27 11:11:51
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 'activity_audit' to your INSTALLED_APPS in settings.py:
```python
INSTALLED_APPS = [
    ...
    'activity_audit',
]
```

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

4. Configure logging in settings.py:
```python
from activity_audit import *

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "json": get_json_formatter(),
        "verbose": get_console_formatter(),
    },
    "handlers": {
        "console": {
            "level": "DEBUG",
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        },
        "file": get_json_handler(level="DEBUG", formatter="json"),
        "api_file": get_api_file_handler(),
        "audit_file": get_audit_handler(),
    },
    "root": {"level": "DEBUG", "handlers": ["console", "file"]},
    "loggers": {
        "audit.request": {
            "handlers": ["api_file"],
            "level": "API",
            "propagate": False,
        },
        "audit.model": {
            "handlers": ["audit_file"],
            "level": "AUDIT",
            "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
### APP Log
```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": ""
}
```

### CRUD Log
```json
{
    "timestamp": "2025-08-16 17:06:32.403",
    "level": "AUDIT",
    "name": "audit.model",
    "message": "CREATE event for User (id: 6f77b814-f9c1-4cab-a737-6677734bc303)",
    "model": "User",
    "event_type": "CREATE",
    "instance_id": "6f77b814-f9c1-4cab-a737-6677734bc303",
    "instance_repr" : {
        "name": "Test Model",
        "is_active": true,
        "created_at": "2025-08-29T08:18:54Z",
        "updated_at": "2025-08-29T08:18:54Z"
    },
    "user_id": "cae8ffb4-ba52-409c-9a6f-e10362bfaf97",
    "user_info": {
        "title": "mr",
        "email": "example@source.com",
        "first_name": "mohamlal",
        "middle_name": "v",
        "last_name": "nair",
        "sex": "m",
    },
    "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",
    "user_id": "14ab1197-ebdd-4300-a618-5910e0219936",
    "user_info": {
        "title": "mr",
        "email": "example@email.com",
        "first_name": "mohanlal",
        "middle_name": "",
        "last_name": "nair",
        "sex": "male",
        "date_of_birth": "21/30/1939"
    },
    "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",
    "user_id": "14ab1197-ebdd-4300-a618-5910e0219936",
    "user_info": {
        "title": "mr",
        "email": "example@email.com",
        "first_name": "mohanlal",
        "middle_name": "",
        "last_name": "nair",
        "sex": "male",
        "date_of_birth": "21/30/1939"
    },
    "request_repr": {
        "endpoint": "example.com",
        "method": "GET",
        "headers": {},
        "body": {}
    },
    "response_repr": {
        "status_code": 200,
        "body": {
            "title": "title",
            "expiresIn": 3600,
            "error": "",
            "errorDescription": ""
        }
    },
    "error_message": "",
    "execution_time": 5.16809344291687
}
```

## 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/a4/31/d6cc31098f24e2ed742ea59f8fd406a464f3fc2f299bfd8381f7e603d969/django_activity_audit-1.2.6.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 'activity_audit' to your INSTALLED_APPS in settings.py:\n```python\nINSTALLED_APPS = [\n    ...\n    'activity_audit',\n]\n```\n\n3. Add the middleware to your MIDDLEWARE in settings.py:\n```python\nMIDDLEWARE = [\n    ...\n    'activity_audit.middleware.AuditLoggingMiddleware',\n]\n```\n\n4. Configure logging in settings.py:\n```python\nfrom 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    },\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(),\n        \"audit_file\": get_audit_handler(),\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        \"audit.model\": {\n            \"handlers\": [\"audit_file\"],\n            \"level\": \"AUDIT\",\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### APP Log\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}\n```\n\n### CRUD Log\n```json\n{\n    \"timestamp\": \"2025-08-16 17:06:32.403\",\n    \"level\": \"AUDIT\",\n    \"name\": \"audit.model\",\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    \"instance_repr\" : {\n        \"name\": \"Test Model\",\n        \"is_active\": true,\n        \"created_at\": \"2025-08-29T08:18:54Z\",\n        \"updated_at\": \"2025-08-29T08:18:54Z\"\n    },\n    \"user_id\": \"cae8ffb4-ba52-409c-9a6f-e10362bfaf97\",\n    \"user_info\": {\n        \"title\": \"mr\",\n        \"email\": \"example@source.com\",\n        \"first_name\": \"mohamlal\",\n        \"middle_name\": \"v\",\n        \"last_name\": \"nair\",\n        \"sex\": \"m\",\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    \"user_id\": \"14ab1197-ebdd-4300-a618-5910e0219936\",\n    \"user_info\": {\n        \"title\": \"mr\",\n        \"email\": \"example@email.com\",\n        \"first_name\": \"mohanlal\",\n        \"middle_name\": \"\",\n        \"last_name\": \"nair\",\n        \"sex\": \"male\",\n        \"date_of_birth\": \"21/30/1939\"\n    },\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    \"user_id\": \"14ab1197-ebdd-4300-a618-5910e0219936\",\n    \"user_info\": {\n        \"title\": \"mr\",\n        \"email\": \"example@email.com\",\n        \"first_name\": \"mohanlal\",\n        \"middle_name\": \"\",\n        \"last_name\": \"nair\",\n        \"sex\": \"male\",\n        \"date_of_birth\": \"21/30/1939\"\n    },\n    \"request_repr\": {\n        \"endpoint\": \"example.com\",\n        \"method\": \"GET\",\n        \"headers\": {},\n        \"body\": {}\n    },\n    \"response_repr\": {\n        \"status_code\": 200,\n        \"body\": {\n            \"title\": \"title\",\n            \"expiresIn\": 3600,\n            \"error\": \"\",\n            \"errorDescription\": \"\"\n        }\n    },\n    \"error_message\": \"\",\n    \"execution_time\": 5.16809344291687\n}\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.2.6",
    "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": "97285add18a3939fee2775509dfbb149fa84289413352ee1c7824bb21a8de63f",
                "md5": "2b70ca8e9cf0ac20c6296800165dffa9",
                "sha256": "ed0dcfdbefdb7339427c62510b72cbb44d635d035f2ee707f0025f160b2339c3"
            },
            "downloads": -1,
            "filename": "django_activity_audit-1.2.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2b70ca8e9cf0ac20c6296800165dffa9",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 16225,
            "upload_time": "2025-10-27T11:11:47",
            "upload_time_iso_8601": "2025-10-27T11:11:47.762510Z",
            "url": "https://files.pythonhosted.org/packages/97/28/5add18a3939fee2775509dfbb149fa84289413352ee1c7824bb21a8de63f/django_activity_audit-1.2.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "a431d6cc31098f24e2ed742ea59f8fd406a464f3fc2f299bfd8381f7e603d969",
                "md5": "14870205d254329c8e097828887adab4",
                "sha256": "9099f252598adab56ccb728232048f2c879a7f5f2a0efefc7f1a0ff8606fb32c"
            },
            "downloads": -1,
            "filename": "django_activity_audit-1.2.6.tar.gz",
            "has_sig": false,
            "md5_digest": "14870205d254329c8e097828887adab4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 57033,
            "upload_time": "2025-10-27T11:11:51",
            "upload_time_iso_8601": "2025-10-27T11:11:51.215687Z",
            "url": "https://files.pythonhosted.org/packages/a4/31/d6cc31098f24e2ed742ea59f8fd406a464f3fc2f299bfd8381f7e603d969/django_activity_audit-1.2.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-27 11:11:51",
    "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.61087s