Django application for logging authentication events (login/logout/failed) in the admin panel. Logs are stored in the database, with the option to save them to a file and output them to the console.
Project description
Django Logger Auth
A Django application for secure authentication event logging with WHOIS lookup and automatic cleanup.
Features
- 🔐 Authentication Event Logging: Automatically logs login, logout, and failed login attempts
- 🌍 WHOIS Integration: Automatic geolocation and organization lookup for IP addresses
- ⚙️ Settings-based Configuration: No database models needed - configure via Django settings
- 📝 Flexible Logging: Support for both file and console logging
- 🧹 Automatic Cleanup: Old log entries are automatically deleted based on retention period
- 🔍 Admin Interface: Full-featured Django admin with filtering, searching, and bulk actions
- ⏰ Timezone Aware: Proper timezone handling and formatting
- 🚀 Performance Optimized: Indexed database queries and efficient cleanup
Installation
Install the package using pip:
pip install django-logger-auth
Or add it to your requirements.txt:
django-logger-auth>=1.0.0
Quick Start
1. Add to INSTALLED_APPS
Add django_logger_auth to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
# ... other apps
'django_logger_auth',
]
2. Configure Settings
Add the configuration dictionary to your settings.py:
DJANGO_LOGGER_AUTH = {
"enable": True, # Enable/disable logging (default: True)
"file_logging": True, # Log to file (default: True)
"console_logging": False, # Log to console/terminal (default: False)
"whois_lookup": True, # Enable WHOIS lookup (default: True)
"keep_days": 30, # Days to keep logs (default: 30)
"log_scope": "admin", # Log scope: "admin" or "all" (default: "admin")
# "admin" - only admin-related authentication events
# "all" - all authentication events
# Admin Navigation Logging
"enable_navigation_logging": True, # Enable/disable navigation logging (default: True)
"navigation_keep_days": 30, # Days to keep navigation logs (default: 30)
"admin_url_prefix": "/admin/", # Admin URL prefix to track (default: "/admin/")
# Change this if your admin is at a different URL
"excluded_paths": [ # List of paths to exclude from logging (default: [])
"/admin/jsi18n/", # Example: exclude JavaScript i18n catalog
],
# Security Settings
"allow_delete": False, # Allow deleting records in admin panel (default: False)
}
3. Add Middleware (for Navigation Logging)
To enable admin navigation logging, add the middleware to your MIDDLEWARE in settings.py:
MIDDLEWARE = [
# ... other middleware
'django_logger_auth.middleware.AdminNavigationLoggingMiddleware',
]
Note: The middleware is optional. If you don't add it, only authentication events will be logged, not navigation.
4. Run Migrations
Create and apply the database migrations:
python manage.py makemigrations
python manage.py migrate
5. Configure Logging (Optional)
If you want file logging, configure a logger in your settings.py:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'auth_events_file': {
'level': 'INFO',
'class': 'logging.FileHandler',
'filename': 'auth_events.log',
},
},
'loggers': {
'auth_events': {
'handlers': ['auth_events_file'],
'level': 'INFO',
'propagate': False,
},
},
}
Usage
Once installed and configured, the app automatically logs authentication events:
- Login Success: When a user successfully logs in
- Logout: When a user logs out
- Login Failed: When a login attempt fails
Viewing Logs
Access the logs through the Django admin interface at /admin/:
- Navigate to Authentication Events
- Use filters to find specific events
- Search by username, IP address, or user agent
- Use bulk actions to delete old records
Admin Navigation Logging
When navigation logging is enabled, the app tracks URLs visited by administrators:
- Configurable admin URL prefix: By default logs
/admin/paths (admin panel), but you can customize this - Excluded paths: You can specify paths to exclude from logging (e.g.,
/admin/jsi18n/) - View navigation logs in Admin Navigation Events at
/admin/django_logger_auth/adminnavigationlog/ - Filter by username, URL path, request method, or status code
- Navigation logs show: timestamp, URL path, URL name, request method, status code, and IP address
Log Format
Log entries are formatted as:
[04/Nov/2025 17:51:26] "LOGIN" user=testuser ip=127.0.0.1 whois=Сountry, City; ua="Mozilla/5.0..."
Admin Actions
- Delete ALL older than Keep days: Removes all log entries older than the configured retention period
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
enable |
bool | True |
Enable or disable authentication logging |
file_logging |
bool | True |
Enable logging to file |
console_logging |
bool | False |
Enable logging to console/terminal |
whois_lookup |
bool | True |
Enable WHOIS lookup for IP addresses |
keep_days |
int | 30 |
Number of days to keep log entries |
log_scope |
str | "admin" |
Log scope: "admin" (only admin-related events) or "all" (all authentication events) |
enable_navigation_logging |
bool | True |
Enable or disable admin navigation logging |
navigation_keep_days |
int | 30 |
Number of days to keep navigation log entries |
admin_url_prefix |
str | "/admin/" |
Admin URL prefix to track (e.g., "/admin/", "/engine/") - auto-detected from Django settings |
excluded_paths |
list | [] |
List of paths to exclude from logging (e.g., ["/admin/jsi18n/"]) |
allow_delete |
bool | False |
Allow deleting records in admin panel (security feature) |
Models
AuthLog
The AuthLog model stores authentication events with the following fields:
username: Username (or "unknown" for failed attempts)ip_address: Client IP addressuser_agent: HTTP User-Agent stringevent_type: Type of event (login, logout, fail)timestamp: When the event occurredwhois_info: WHOIS information (country, city, organization)
AdminNavigationLog
The AdminNavigationLog model stores admin panel navigation events with the following fields:
username: Username of the userurl_path: The URL path visitedurl_name: Resolved Django URL name (if available)ip_address: Client IP addressuser_agent: HTTP User-Agent stringtimestamp: When the navigation occurredrequest_method: HTTP method (GET, POST, etc.)status_code: HTTP response status code
Automatic Cleanup
The app automatically deletes old log entries when new events are created:
- Authentication logs are cleaned up based on the
keep_dayssetting - Navigation logs are cleaned up based on the
navigation_keep_dayssetting
Performance Considerations
- Database indexes are automatically created on
timestamp,event_type, andip_address - Cleanup is performed in batches to avoid blocking
- WHOIS lookups are synchronous but can be disabled for better performance
Requirements
- Python 3.10+
- django>=5.0
- requests>=2.32.5
- pytz>2025.2
- ipwhois>=1.3.0
Troubleshooting
WHOIS Lookup Failures
If WHOIS lookups are failing, you can disable them:
DJANGO_LOGGER_AUTH = {
"whois_lookup": False,
# ... other settings
}
Logs Not Appearing
- Check that
enableis set toTruein settings - Verify the app is in
INSTALLED_APPS - Check that migrations have been run
- Ensure logging is configured if using file logging
Performance Issues
- Disable WHOIS lookup if not needed
- Reduce
keep_daysto keep fewer records - Use database indexes (automatically created)
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
See CHANGELOG.md for a list of changes and version history.
Support
For issues, questions, or contributions, please open an issue on GitHub.
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_logger_auth-1.2.0.tar.gz.
File metadata
- Download URL: django_logger_auth-1.2.0.tar.gz
- Upload date:
- Size: 17.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2f9c54e24b798834394c0e2f5e23b55fa32217d216eb05b2e7014cb91b899d9d
|
|
| MD5 |
3f0f83b4b63c85eee91d307e2548362a
|
|
| BLAKE2b-256 |
d37e3ee1932b058e33b00032ad6752fe274a1b2eeccfb186e0aa119ae0d2970f
|
Provenance
The following attestation bundles were made for django_logger_auth-1.2.0.tar.gz:
Publisher:
release.yml on devops-regulus/django-logger-auth
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_logger_auth-1.2.0.tar.gz -
Subject digest:
2f9c54e24b798834394c0e2f5e23b55fa32217d216eb05b2e7014cb91b899d9d - Sigstore transparency entry: 957471310
- Sigstore integration time:
-
Permalink:
devops-regulus/django-logger-auth@3e01e75614158f57a7eb85b1ed9bf928d4677222 -
Branch / Tag:
refs/tags/v1.2.0 - Owner: https://github.com/devops-regulus
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@3e01e75614158f57a7eb85b1ed9bf928d4677222 -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_logger_auth-1.2.0-py3-none-any.whl.
File metadata
- Download URL: django_logger_auth-1.2.0-py3-none-any.whl
- Upload date:
- Size: 15.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ca05891fb5715ae4e82b5d238ce3b0d5c6e85c7c887ff3a21904e761de413e9
|
|
| MD5 |
05aebc46700c913516c9f0de468fa819
|
|
| BLAKE2b-256 |
3858f880808a50ebcab80b9be120abe3d8a113b17cffa86656bdd19cc40a3017
|
Provenance
The following attestation bundles were made for django_logger_auth-1.2.0-py3-none-any.whl:
Publisher:
release.yml on devops-regulus/django-logger-auth
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_logger_auth-1.2.0-py3-none-any.whl -
Subject digest:
8ca05891fb5715ae4e82b5d238ce3b0d5c6e85c7c887ff3a21904e761de413e9 - Sigstore transparency entry: 957471312
- Sigstore integration time:
-
Permalink:
devops-regulus/django-logger-auth@3e01e75614158f57a7eb85b1ed9bf928d4677222 -
Branch / Tag:
refs/tags/v1.2.0 - Owner: https://github.com/devops-regulus
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@3e01e75614158f57a7eb85b1ed9bf928d4677222 -
Trigger Event:
release
-
Statement type: