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

Uploaded Python 3

File details

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

File metadata

  • Download URL: loki_django_logger-1.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 4d9e670042d54a29fda125e2ee732803abe76e21fb939b50ebdf4d6c25c32f12
MD5 dfff5a549ddfbda6cd3c3fbe11d3bbd7
BLAKE2b-256 09a5c77d906bde70dfa37684be72e602688663493a9c81e255b6f4748ee064a4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for loki_django_logger-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 be8b7c59bd7d68c3628a83fdbc81369844b7aed5e9e77f003672598b83b74123
MD5 358e2c7d6974b4ea9ad161808d2e25cb
BLAKE2b-256 3f497f9019f03fe6baec8c3ce093ca1ccb276c03caf349f217f5572350c777d6

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