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.2.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.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: loki_django_logger-1.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 d0f8ddacf1c01bbb7a724166b955cbb79c9c9e6cd368efb2030f7c3fe9609224
MD5 c8240bef1d0398c40201076f395d06b1
BLAKE2b-256 b06923e1ec6167c44d4da25b6d79daca3dd299cc2f1e4651f9e865affd5bc801

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for loki_django_logger-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3748ffc4d79a616679ccac9a8227c22473c3294e9658637537e8c2bdcd692ade
MD5 bcf0faabdcfb351b1a6603f1377a9823
BLAKE2b-256 ca2619fd92f3925b5b65cd733a844c120ac8146bd4818927cb330aa3b54fc804

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