Skip to main content

Asynchronous Loki logger for Django applications

Project description

Loki Django Logger

Loki Django Logger is a lightweight and efficient logger designed to send logs to Grafana Loki directly from your Django application with minimal latency and low resource consumption. It supports dynamic configurations, timeout management, and efficient logging using asyncio and httpx.

Features

  • Asynchronous log transmission for improved performance
  • Configurable timeout to prevent delayed requests
  • Middleware for capturing request/response data with latency tracking
  • Customizable tags for better log organization in Loki
  • Robust error handling with retries and exponential backoff

Installation

pip install loki-django-logger

Configuration

1. Import the Logger and Middleware

In your Django project, import the required components in settings.py:

import os  # Required for environment variables
from loki_django_logger.logger import LokiLoggerHandler
from loki_django_logger.middleware import LokiLoggerMiddleware

2. Add Middleware

Add the following middleware to your settings.py:

MIDDLEWARE = [
    ...,
    "loki_django_logger.middleware.LokiLoggerMiddleware",
]

3. Add Logging Configuration

Add the Loki logger configuration in settings.py:

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "loki": {
            "level": "INFO",
            "class": "loki_django_logger.logger.LokiLoggerHandler",
            "loki_url": os.getenv("LOKI_URL", "http://localhost:3100/loki/api/v1/push"),
            "tags": {"app": "django", "environment": "production"},
            "timeout": os.getenv("LOGGING_TIMEOUT", "1"),
        },
    },
    "root": {
        "handlers": ["loki"],
        "level": "INFO",
    },
}

4. Environment Variables (Optional)

For improved flexibility, consider using environment variables for sensitive information like the loki_url or timeout.

LOKI_URL=http://localhost:3100/loki/api/v1/push
LOGGING_TIMEOUT=1

Usage

Once integrated, logs will automatically be sent to your Loki instance with the following details:

  • API endpoint path
  • HTTP method
  • Response status code
  • Time taken for the request
  • Client IP address
  • User-agent headers for better client identification

Example Log Output

{
    "path": "/api/v1/patient",
    "method": "POST",
    "status_code": 200,
    "duration": 0.123,
    "client_ip": "192.168.1.100",
    "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
}

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.

License

This project is licensed under the MIT License.

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

loki_django_logger-1.0.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

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

loki_django_logger-1.0.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file loki_django_logger-1.0.1.tar.gz.

File metadata

  • Download URL: loki_django_logger-1.0.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for loki_django_logger-1.0.1.tar.gz
Algorithm Hash digest
SHA256 1a09847faf930c0bfaa57dc7dbe68c260b6e2f4e2bb750831c2a1240c23e1943
MD5 82f6db00efd777777bb8e4db9f9f52b6
BLAKE2b-256 647ef8834cdb98b73262e45ff3ead7293c07c4a6a9c16753f1f34449a3aa3ac7

See more details on using hashes here.

File details

Details for the file loki_django_logger-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for loki_django_logger-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8d555ac9cd70053b9a1aa30f5d8e027bb06adcd0b71c3c3d299a04b442a2bd87
MD5 807f52f94236eddd8588d00afa18a9b8
BLAKE2b-256 a878760d0c8376304b334c2aa3a8cef9e6854af1aa418d576d9c9b49b4a4addd

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