Django middleware for request analytics logging
Project description
django-analytics-middleware
One middleware. Audit logs for compliance. Analytics for product.
Status: 🏗️ Building in public
This package is under continuous development. And in production.
What it will do (already working locally)
- Log every request (path, method, status, user, IP, user agent)
- Filter noise paths (/admin, /health, /static)
- Duration tracking in milliseconds
- IP extraction behind proxies (X-Forwarded-For)
- Configurable log file path
- Django admin view for logs
- Export to CSV/JSON command
- Tests (coming this week)
- PyPI release
Features
- 📊 Logs all HTTP requests with timing data
- 👤 Captures authenticated user UUIDs
- 🌐 Extracts real IP addresses (handles proxies)
- 🚫 Skips configurable noise paths (health checks, admin, etc.)
- 📝 Writes JSON logs for easy parsing
- ⚡ Minimal performance impact
- 🔒 Never breaks your application if logging fails
Want to help?
- Open an issue with your wishlist
- Star the repo to follow progress
- DM me on LinkedIn
Installation
pip install django-analytics-middleware
# Add to INSTALLED_APPS
INSTALLED_APPS = [
...
'django_analytics_middleware',
]
# Add to MIDDLEWARE
MIDDLEWARE = [
...
'django_analytics_middleware.middleware.AnalyticsMiddleware',
]
# Configure log path
ANALYTICS_LOG_PATH = os.path.join(BASE_DIR, 'logs', 'analytics.log')
# For .env in settings.py
ANALYTICS_LOG_PATH = ANALYTICS_LOG_PATH=<env_handler>(<LOG PATH>)
Configuration
Required Settings
Setting Description Example
ANALYTICS_LOG_PATH Where to write log files - os.path.join(BASE_DIR, 'logs', 'analytics.log')
Optional Settings
Setting Default Description
ANALYTICS_NOISE_PATHS ['/health', '/admin', '/favicon.ico'] Paths to skip logging
ANALYTICS_LOG_LEVEL 'WARNING' # Logging level for the package
# Example settings.py
# Required
ANALYTICS_LOG_PATH = os.path.join(BASE_DIR, 'logs', 'analytics.log')
# Optional - Custom noise paths
ANALYTICS_NOISE_PATHS = [
'/health',
'/metrics',
'/admin',
'/favicon.ico',
'/robots.txt'
]
# Optional - Set log level (DEBUG, INFO, WARNING, ERROR)
ANALYTICS_LOG_LEVEL = 'INFO'
# Log Format
{
"path": "/api/users/",
"method": "GET",
"status": 200,
"user_uuid": "550e8400-e29b-41d4-a716-446655440000",
"duration_ms": 45,
"ip": "192.168.1.100",
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
"referrer": "https://example.com/",
"timestamp": "2024-01-15T10:30:45.123456+00:00",
"month": "Jan",
"week": "03",
"day": "Mon",
"hour": "10"
}
# Log Field Descriptions
Field Type Description
path string Request URL path
method string HTTP method (GET, POST, PUT, DELETE, etc.)
status integer HTTP response status code
user_uuid string Authenticated user's UUID or "unauthorized user"
duration_ms integer Request processing time in milliseconds
ip string Client IP address (handles proxy forwarding)
user_agent string Browser/device user agent string
referrer string Referring URL (where user came from)
timestamp string ISO 8601 timestamp with timezone
month string Three-letter month abbreviation (Jan, Feb, Mar)
week string ISO week number (01-53)
day string Three-letter day abbreviation (Mon, Tue, Wed)
hour string Hour in 24-hour format (00-23)
## License
MIT – Because audit logs shouldn't be paywalled.
Project details
Release history Release notifications | RSS feed
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 django_analytics_middleware-0.1.1.tar.gz.
File metadata
- Download URL: django_analytics_middleware-0.1.1.tar.gz
- Upload date:
- Size: 10.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0f6420797c6a1bde305af34d0ffdc62d01071d9a14f970f6e63bb0031085505
|
|
| MD5 |
b214f1109040d98b71d4bd3fa6ebc57b
|
|
| BLAKE2b-256 |
140a346f5500cdffabd025da53bb6e15602a233a370039aa32c80b875600aed8
|
File details
Details for the file django_analytics_middleware-0.1.1-py3-none-any.whl.
File metadata
- Download URL: django_analytics_middleware-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
509085413724c37fe5348c05f92568150d7d6d4860a77723cb71913e566d0a15
|
|
| MD5 |
d90e7e126392df0345e515e9e5529698
|
|
| BLAKE2b-256 |
517230b9ec99aa641f0af972c6a2ecd522db7f47ce8b2b6c479ee28dc02aebb5
|