Skip to main content

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
}

3. Run Migrations

Create and apply the database migrations:

python manage.py makemigrations
python manage.py migrate

4. 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/:

  1. Navigate to Authentication Events
  2. Use filters to find specific events
  3. Search by username, IP address, or user agent
  4. Use bulk actions to delete old records

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 or all (default: "admin") admin - only admin-related authentication events, all - all authentication events

Models

AuthLog

The AuthLog model stores authentication events with the following fields:

  • username: Username (or "unknown" for failed attempts)
  • ip_address: Client IP address
  • user_agent: HTTP User-Agent string
  • event_type: Type of event (login, logout, fail)
  • timestamp: When the event occurred
  • whois_info: WHOIS information (country, city, organization)

Automatic Cleanup

The app automatically deletes old log entries when new events are created. The retention period is controlled by the keep_days setting.

Performance Considerations

  • Database indexes are automatically created on timestamp, event_type, and ip_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

  1. Check that enable is set to True in settings
  2. Verify the app is in INSTALLED_APPS
  3. Check that migrations have been run
  4. Ensure logging is configured if using file logging

Performance Issues

  • Disable WHOIS lookup if not needed
  • Reduce keep_days to 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


Download files

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

Source Distribution

django_logger_auth-1.1.0.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

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

django_logger_auth-1.1.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file django_logger_auth-1.1.0.tar.gz.

File metadata

  • Download URL: django_logger_auth-1.1.0.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_logger_auth-1.1.0.tar.gz
Algorithm Hash digest
SHA256 6312d96679fe2d1707fe92e9edef30ffc7404a22a41222d58ecb98fd0a2cdf50
MD5 da22d3704c615ab66fd89e1cf6029cdf
BLAKE2b-256 dfbf1ad4246373644c26e5a764e8f0ac76bf81c0df3dd73bb4a6b1766bc1d160

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_logger_auth-1.1.0.tar.gz:

Publisher: release.yml on devops-regulus/django-logger-auth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_logger_auth-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_logger_auth-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a8f459927fb55a06d6e0b9e64dd5768b3e775e37891b25a925d32f112ca9c99d
MD5 01dadadeb95c434857b4d909255b4f75
BLAKE2b-256 bc552c7cebdecf32e60b6388dcfc2795817150189e7abbd65d7d31335b3fc72e

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_logger_auth-1.1.0-py3-none-any.whl:

Publisher: release.yml on devops-regulus/django-logger-auth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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