A Django application that logs HTTP 500 errors with detailed context
Project description
Django Error Logger
Keep It Simple and Stupid replacement of Sentry for error logging in django project.
A Django application that logs HTTP 500 errors to the database, capturing detailed context including user information, request details, and POST data.
Features
- Automatic Error Logging: Captures all unhandled exceptions (HTTP 500 errors)
- User Context: Logs the authenticated user when the error occurred
- Request Details: Captures path, method, headers, IP address, and query string
- POST Data Logging: Saves POST/PUT/PATCH payloads with sensitive field redaction
- Large Payload Protection: Filters out POST payloads larger than 100KB
- HTML Traceback: Stores Django's debug error page HTML for detailed debugging
- User Impersonation: Link to impersonate the user and reproduce the error (optional)
- Django Admin Integration: View and manage error logs through Django admin
Installation
Method 1: Install from PyPI (Recommended)
From any Django project:
pip install django-error-logger
🚀 Quick Integration
After installation, integrate into your Django project:
Step 1: Add to INSTALLED_APPS
Edit settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# ... your other apps ...
'error_logger', # Add this
]
Step 2: Add Middleware
Edit settings.py:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
# ... your other middleware ...
'error_logger.middleware.ErrorLoggingMiddleware', # Add at the end
]
Step 3: Add URLs
Edit your main urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
# ... your other URLs ...
path('error-logger/', include('error_logger.urls')), # Add this
]
Step 4: Run Migrations
python manage.py migrate error_logger
Step 5: Test It
# Start your Django server
python manage.py runserver
# Trigger a test error (development only!)
curl http://localhost:8000/error-logger/test/500/
# Check Django admin
open http://localhost:8000/admin/error_logger/errorlog/
Step 6: User Permissions
To access the error logger views (such as the error log list, details, and test endpoints), users must be logged in and have the error_logger.view_errorlog permission. This permission is automatically created when you run migrations.
Assigning Permissions
You can assign this permission to users or groups via the Django admin interface:
- Go to the Django admin at /admin/.
- Navigate to Users or Groups.
- Select a user or group.
- In the permissions section, check the box for Can view error log under the Error logger app.
- Save the changes.
Alternatively, you can assign permissions programmatically in your code or via fixtures.
Note: Only users with this permission can access the error logger URLs. Ensure your authentication system is properly configured.
🔧 Optional: User Impersonation Feature
To enable the "impersonate user" feature in admin:
pip install django-su
Then add to settings.py:
INSTALLED_APPS = [
# ...
'django_su',
'error_logger',
]
And add to urls.py:
urlpatterns = [
# ...
path('su/', include('django_su.urls')),
]
⚠️ Production Considerations
Before deploying to production:
- Remove Test Endpoints: Comment out or remove test views in
error_logger/views.py - Access Control: Ensure only admin users can access
/error-logger/URLs - Log Cleanup: Implement a strategy to clean up old error logs
- Review Sensitive Fields: Customize the
SENSITIVE_FIELDSlist for your app - Database Size: Monitor error log table size
Configuration
Sensitive Fields
The following fields are automatically redacted in POST data logs:
password,password1,password2,old_password,new_passwordtoken,access_token,refresh_token,api_key,secretcredit_card,cvv,ssn,pin
You can customize this list in middleware.py:
SENSITIVE_FIELDS = [
'password', 'token', 'api_key', 'secret',
# Add your custom sensitive fields here
]
POST Data Size Limit
By default, POST payloads larger than 100KB are not logged in full. You can adjust this in middleware.py:
MAX_POST_SIZE = 100 * 1024 # 100KB (default)
# MAX_POST_SIZE = 1024 * 1024 # 1MB
Usage
Django Admin
Access the error logs through Django admin at /admin/error_logger/errorlog/
Features:
- List view with error type, time, user, path, and method
- Filter by error type, time, and method
- Search by error message, path, or username
- Detail view with full traceback and HTML error page
- "Open in Full Page" button to view the HTML traceback
- "Impersonate and visit error page" button (requires
django_su)
Test Endpoints
⚠️ Important: These endpoints should only be accessible in development/testing environments. Remove or restrict access in production.
Access these endpoints at /error-logger/test/:
1. Simple 500 Error
URL: /error-logger/test/500/
Triggers a ZeroDivisionError to test basic error logging.
curl http://localhost:8000/error-logger/test/500/
2. ValueError Test
URL: /error-logger/test/value-error/
Triggers a ValueError with a custom message.
curl http://localhost:8000/error-logger/test/value-error/
3. KeyError Test
URL: /error-logger/test/key-error/
Triggers a KeyError by accessing a non-existent dictionary key.
curl http://localhost:8000/error-logger/test/key-error/
4. POST Error Test
URL: /error-logger/test/post-error/
Tests error logging with POST data including sensitive fields (password, token).
Form Fields:
username: Regular text fieldpassword: Will be redacted in logsemail: Email fieldtoken: Will be redacted in logs
Usage:
- Visit the URL in your browser
- Fill out the form (pre-populated with test data)
- Submit the form to trigger an error
- Check Django admin to see POST data with sensitive fields redacted
cURL Example:
curl -X POST http://localhost:8000/error-logger/test/post-error/ \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=testuser&password=secret123&email=test@example.com&token=abc123"
5. Large POST Error Test
URL: /error-logger/test/large-post-error/
Tests error logging with a large POST payload (150KB) that exceeds the logging limit.
Form Fields:
username: Regular text fieldpassword: Will be redacted in logslarge_field: Pre-filled with 150KB of data
Usage:
- Visit the URL in your browser
- Submit the form (large_field is pre-populated)
- Check Django admin to see that POST data size is logged but content is not
Expected Result:
The error log's additional_info should contain:
{
"post_data": {
"error": "POST payload too large to log",
"size_bytes": 153600,
"size_kb": 150.0,
"size_mb": 0.15
}
}
Model Structure
ErrorLog Model
| Field | Type | Description |
|---|---|---|
user |
ForeignKey | User who encountered the error (null if anonymous) |
error_message |
TextField | Plain text exception message |
error_type |
CharField | Exception class name (e.g., "ValueError") |
traceback |
TextField | Plain text Python traceback |
html_traceback |
TextField | HTML version of Django's debug page |
error_time |
DateTimeField | When the error occurred |
path |
CharField | Request path |
method |
CharField | HTTP method (GET, POST, etc.) |
user_agent |
CharField | User's browser/client |
ip_address |
GenericIPAddressField | Client IP address |
query_string |
TextField | URL query parameters |
additional_info |
JSONField | POST data and request headers |
Security Considerations
- Sensitive Data: Always review the
SENSITIVE_FIELDSlist to ensure all sensitive data in your application is redacted - Access Control: Error logs contain sensitive information. Ensure only authorized users (admin group) can access them
- Test Endpoints: Remove or restrict access to test endpoints in production
- Database Storage: Error logs can accumulate. Implement a cleanup strategy (e.g., delete logs older than 90 days)
- HTML Traceback: Contains full request context. Ensure admin access is properly secured
Requirements
- Django 3.2+
- Python 3.8+
django_su(for user impersonation feature)
License
AGPL-3.0
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_error_logger-1.0.1.tar.gz.
File metadata
- Download URL: django_error_logger-1.0.1.tar.gz
- Upload date:
- Size: 26.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb1bdf277752a7d10f9ad4a4a0d2b9f88c011b6854f99bd09a1dd5d5b479c928
|
|
| MD5 |
f6bf632c0df3040c19b6f28873f95ff5
|
|
| BLAKE2b-256 |
3e963e16b1fde94de0463a2890d9df50ce232ee215487b28841a06fb0d9af127
|
File details
Details for the file django_error_logger-1.0.1-py3-none-any.whl.
File metadata
- Download URL: django_error_logger-1.0.1-py3-none-any.whl
- Upload date:
- Size: 29.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc8bb6dc08954de4671c59dcd3d6a007428de03031436fafb1441211a7cda36b
|
|
| MD5 |
26f1d489993dd8fdbd5f5d73ecfdfc35
|
|
| BLAKE2b-256 |
0a493ebbc7c5baee4ac298e6565717b77ee6f96ce762c66c1f9d7fddf0bb11be
|