Skip to main content

Django middleware for request analytics logging

Project description

django-audit-analytics-middleware

One middleware. Audit logs for compliance. Analytics for product.

Status: 🏗️ Building in public

This package is under active development. I'm posting every step on LinkedIn.

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_analytics_middleware-0.1.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

django_analytics_middleware-0.1.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file django_analytics_middleware-0.1.0.tar.gz.

File metadata

File hashes

Hashes for django_analytics_middleware-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fcfb43604a3928c8b690e43d78728888dc617dc1c0cc7284ae945f56176b4e27
MD5 7d79d18f54bb62fac76f5f7fe69518b5
BLAKE2b-256 61e58b39a319734ec612bcc0859bd45e20842d94f0a2b69523eecf38d17992fc

See more details on using hashes here.

File details

Details for the file django_analytics_middleware-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_analytics_middleware-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e589318e3c9662c855dfcf2d1ad8904c5a4e79c40431402948342e4c2588b5f8
MD5 f6c42dc8bcc71c2f328398e263efac52
BLAKE2b-256 3a446228b7fb2afd2bd2545ef63593a5fbdc5675d94e743ec6d5207522615abb

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