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

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:

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "loki": {
            "level": "INFO",
            "class": "loki_django_logger.logger.LokiLoggerHandler",
            "loki_url": "http://localhost:3100/loki/api/v1/push",
            "tags": {"app": "django", "environment": "production"},
            "timeout": "1",  # Timeout in seconds
        },
    },
    "root": {
        "handlers": ["loki"],
        "level": "INFO",
    },
}

3. 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

Example Log Output

{
    "path": "/api/v1/patient",
    "method": "POST",
    "status_code": 200,
    "duration": 0.123,
    "client_ip": "192.168.1.100"
}

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.0.tar.gz (4.2 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.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: loki_django_logger-1.0.0.tar.gz
  • Upload date:
  • Size: 4.2 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.0.tar.gz
Algorithm Hash digest
SHA256 70dfb197323d9260912e4a6d837820925eef734674596700285f1451c64ab910
MD5 c82cc4ee4b1984bb0e484414f0d873bd
BLAKE2b-256 a3d53077293bfe3e07b63e7aa9a5096f60a9f70d72ab1af6b1e6dbe221a076a2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for loki_django_logger-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 80d415f57f05cc417e58c296c9ba89933a53db133dbf136959445daea6df9d0c
MD5 a60483820a38b086d13dc92fe512f91b
BLAKE2b-256 68adeec44ff5c7bf8f19d584c7e3ceb904406450e4e7f4534d312fd4456c849a

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