Skip to main content

A Django application for logging and displaying HTTP requests.

Project description

Django Request Logs

Django Request Logs is a reusable Django app designed to log and display HTTP requests in your project. This application allows developers to monitor incoming requests, their metadata, and responses for debugging and analysis.

Features

  • Logs HTTP requests (method, path, status code, and duration).
  • Provides middleware for lightweight logging.
  • Easy-to-use admin interface to view logged requests.
  • Flexible views and templates for displaying logs.

Installation

Follow these steps to install and integrate Django Request Logs into your Django project:

Step 1: Install the Package

Install the app using pip:

pip pip install django_requests_loger

Step 2: Add to INSTALLED_APPS

Update your Django settings file (settings.py) to include the app:

INSTALLED_APPS = [
    ...,
    'django_requests_loger',
]

Step 3: Add Middleware

Insert the RequestLoggingMiddleware into the MIDDLEWARE list in your settings file:

MIDDLEWARE = [
    ...,
    'django_requests_loger.middleware.RequestLoggingMiddleware',
]

Step 4: Add Url

Add the url in project urls.py file:

urlpatterns = [
    path('', include('django_requests_loger.urls')),  # Include app URLs
]

Step 5: Run Migrations

Apply database migrations to create necessary tables:

python manage.py migrate

Dynamic Login Page Customization

Django Request Logs also includes a dynamic login page that you can customize without modifying the template code.

  1. Add the Context Processor Ensure your project’s TEMPLATES configuration in settings.py includes the custom context processor:
   TEMPLATES = [
    {
        # ... other template settings ...
        "OPTIONS": {
            "context_processors": [
                # ... default context processors ...
                "django_requests_loger.context_processors.dynamic_login_settings",
                "django_requests_loger.context_processors.ui_settings",
            ],
        },
    },
]
  1. Override Default Values You can override the default login page settings by adding these variables to your settings.py:
LOGIN_PAGE_LOGO = "images/MyCustomLogo.png"  # Path to your logo in your static files
LOGIN_PAGE_HEADING = "WELCOME TO MY CUSTOM PROJECT <br>(Dynamic Login)"

If these are not set, the context processor will fall back to the default values.

  1. Template Usage The login template uses these context variables to display dynamic content. For example:
<img src="{% static LOGIN_PAGE_LOGO %}" alt="Logo">
<h1>{{ LOGIN_PAGE_HEADING|safe }}</h1>

The |safe filter allows HTML tags (like <br>) to be rendered correctly.

  1. Dynamic Request Logger Settings You can control excluded paths, prefixes, and regex patterns for logging in settings.py:
REQUEST_LOGGER = {
    "EXCLUDED_PATHS": [
        "/django_requests_loger/*",
        "/",
        "/django_requests_loger/",
        "/fetch-latest-logs/",
    ],
    "EXCLUDED_PREFIXES": [
        "/fetch-latest-logs/",
    ],
    "EXCLUDED_REGEX_PATTERNS": [
        r"^/django_requests_loger/\d+/$",
    ],
    # Add more keys as needed...
}
  1. Advanced View Configuration DJANGO_REQUESTS_LOGER_CONFIG controls session timeouts, default pagination, and your “section map” for dynamic views:
DJANGO_REQUESTS_LOGER_CONFIG = {
    "SECTION_MAP": {
        "commands": "Commands",
        "schedule": "Schedule",
        "jobs": "Jobs",
        "batches": "Batches",
        "cache": "Cache",
        "dumps": "Dumps",
        "events": "Events",
        "exceptions": "Exceptions",
        "gates": "Gates",
        "http-client": "HTTP Client",
        "mail": "Mail",
        "models": "Models",
        "notifications": "Notifications",
        "queries": "Queries",
        "redis": "Redis",
        "views": "Views",
    },
    "SESSION_TIMEOUT_HOURS": 12,  # default 12 hours
    "DEFAULT_LOG_OFFSET": 0,
    "DEFAULT_LOG_LIMIT": 100,
}
  1. Dynamic UI Settings django_requests_loger.context_processors.ui_settings enables branding and sidebar customization:
DJANGO_REQUESTS_LOGER_UI = {
    "BRAND_LOGO": "images/Logo.png",  # Path in static files
    "PROJECT_NAME": "Django Log Requests",
    "SIDEBAR_LINKS": [
        {"url": "/request-logs/", "label": "Requests", "icon": "bi-graph-up"},
        {"url": "/commands/", "label": "Commands", "icon": "bi-terminal"},
        {"url": "/schedule/", "label": "Schedule", "icon": "bi-calendar-event"},
        {"url": "/jobs_view/", "label": "Jobs", "icon": "bi-briefcase"},
        {"url": "/batches/", "label": "Batches", "icon": "bi-box-seam"},
        {"url": "/cache/", "label": "Cache", "icon": "bi-database"},
        {"url": "/dumps/", "label": "Dumps", "icon": "bi-download"},
        {"url": "/events/", "label": "Events", "icon": "bi-lightning-fill"},
        {"url": "/exceptions/", "label": "Exceptions", "icon": "bi-exclamation-circle"},
        {"url": "/gates/", "label": "Gates", "icon": "bi-shield-lock"},
        {"url": "/http-client/", "label": "HTTP Client", "icon": "bi-globe"},
        {"url": "/mail/", "label": "Mail", "icon": "bi-envelope"},
        {"url": "/models/", "label": "Models", "icon": "bi-file-earmark"},
        {"url": "/notifications/", "label": "Notifications", "icon": "bi-bell"},
        {"url": "/queries/", "label": "Queries", "icon": "bi-card-list"},
        {"url": "/redis/", "label": "Redis", "icon": "bi-hdd"},
        {"url": "/views/", "label": "Views", "icon": "bi-eye"},
    ]
}

In your base template, you can reference these variables (e.g., {{ BRAND_LOGO }}, {{ PROJECT_NAME }}, SIDEBAR_LINKS) to dynamically render your sidebar or header.

Usage

Admin Panel

Logged requests can be viewed and managed through the Django admin interface:

  1. Navigate to /admin/.
  2. Look for the Request Logs section.

Fetch Logs via API

Retrieve the latest logs using the provided API endpoint:

GET /fetch-latest-logs/

Example response:

{
  "logs": [
    {
      "id": 1,
      "method": "GET",
      "path": "/some-path/",
      "status_code": 200,
      "timestamp": "2024-11-29T12:00:00Z"
    }
  ]
}

Customizing Templates

The app includes default templates for displaying logs. These can be overridden by placing templates with the same name in your project's templates/ directory:

  • request_logs.html
  • request_log_detail.html

Development

Running Tests

Ensure the app works as expected by running the test suite:

python manage.py test django_requests_loger

Contributing

  1. Fork the repository.
  2. Create a feature branch:
    git checkout -b feature-name
    
  3. Commit your changes and push them:
    git commit -m "Add feature-name"
    git push origin feature-name
    
  4. Submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Support

For questions or support, please contact: mominalikhoker589@gmail.com.

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-requests-loger-1.1.7.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

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

django_requests_loger-1.1.7-py3-none-any.whl (26.4 kB view details)

Uploaded Python 3

File details

Details for the file django-requests-loger-1.1.7.tar.gz.

File metadata

  • Download URL: django-requests-loger-1.1.7.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.20

File hashes

Hashes for django-requests-loger-1.1.7.tar.gz
Algorithm Hash digest
SHA256 0a8a49ab6bfa9ecc00f21c3da776b5e3cc523d51de223c4b5d13bf6d792ad68b
MD5 4b05947708a6318500551cbe9d02d3d9
BLAKE2b-256 25f22adbd8fd8bb40d4847b44bd1293e73ded37df7f97d8996ad60f8b85ba3ed

See more details on using hashes here.

File details

Details for the file django_requests_loger-1.1.7-py3-none-any.whl.

File metadata

File hashes

Hashes for django_requests_loger-1.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 13657bed8edc2172c1aa4ab30851e01bed5de40e4b94c21c9ca6dbef72ed271a
MD5 c1687f9a22c388771fc92ac5c2a0904e
BLAKE2b-256 b4759e924d66adff9b56f145145c796a0c60a4b950217c67fd7589f61a0ca778

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