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. Add Middleware

Add the following middleware to your settings.py:

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

2. Add Logging Configuration

Add the Loki logger configuration in settings.py:

import os
import logging
from loki_django_logger.logger import LokiLoggerHandler
from loki_django_logger.middleware import LokiLoggerMiddleware

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "loki": {
            "level": "INFO",
            "class": "loki_django_logger.logger.LokiLoggerHandler",
            "loki_url": os.getenv("LOKI_URL", "https://loki.test.xyz/loki/api/v1/push").rstrip('/'),
            "tags": {
                "app": os.getenv("APP_NAME", "test"),
                "environment": os.getenv("ENVIRONMENT", "test")
            },
            "timeout": float(os.getenv("LOGGING_TIMEOUT", "1")),
        },
    },
    "root": {
        "handlers": ["loki"],
        "level": "INFO",
    },
}

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.4.tar.gz (4.5 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.4-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: loki_django_logger-1.0.4.tar.gz
  • Upload date:
  • Size: 4.5 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.4.tar.gz
Algorithm Hash digest
SHA256 7cc596282ae60a9bea643b6c8856eced28eb84abdf6974851a886ed028f78768
MD5 5c28238daa54d40a9347bfdfcfc4975a
BLAKE2b-256 0d6fc03d3824eecd5265d4fafc366bcf762d45825f899684b9083e0d7aefa60e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for loki_django_logger-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 5a4c96f62377de79192c287941d22ef6960aeb89a4ddc9b3ce8a8eeb05241552
MD5 20367a83e79a46a743fc9246a20acf1b
BLAKE2b-256 f77f725d9ec89a0f56fa814b775435a42ee8b88017f0bb3f617f87fe521b0856

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