Skip to main content

Loki logger for Django applications

Project description

Loki Django Logger

A lightweight logging solution for Django applications that sends logs to Grafana Loki with gzip compression for improved performance.

🚀 Installation

Install the package using pip:

pip install loki-django-logger

⚙️ Configuration

Add the logger to your Django settings

In your settings.py file:

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "verbose": {
            "format": "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            "datefmt": "%Y-%m-%d %H:%M:%S",
        },
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        },
        "loki": {
            "class": "loki_django_logger.handler.AsyncGzipLokiHandler",
            "loki_url": "https://loki.test.dev",
            "labels": {"application": "django-app", "environment": "development"},
            "level": "DEBUG",
            "flush_interval": 1,
        },
    },
    "loggers": {
        "django": {
            "handlers": ["console", "loki"],
            "level": "INFO",
            "propagate": False,
        },
    },
}

Install Loki (if not already available)

docker run -d --name=loki -p 3100:3100 grafana/loki:latest

Run your Django application and monitor the logs in Loki.


📝 Example Usage

In your Django views or tasks:

import logging
logger = logging.getLogger("django")

def sample_view(request):
    logger.info("Sample log message sent to Loki", extra={"user_id": 123, "operation": "sample_view"})
    return JsonResponse({"message": "Logged successfully!"})

🧪 Testing

To run tests:

pytest tests/

📜 License

This project is licensed under the MIT License. See the LICENSE file for details.

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: loki_django_logger-1.0.9.tar.gz
  • Upload date:
  • Size: 5.0 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.9.tar.gz
Algorithm Hash digest
SHA256 ad48d5fb7882219c028e50644ef2719446bfccd1e8e22276832afd9ed71a4681
MD5 6fe659109c2539f120c879200e954ad8
BLAKE2b-256 e6f4bc189a508a17c6f730513758ca446409baff91195d52f9ae5fc29777e5a4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for loki_django_logger-1.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 637cfbf2e1b96d4e9bccd4e27d174ee9325b82e3ab2e89705a24487bc6edd897
MD5 22674e7a8a635689c7e987f96c4f7ae0
BLAKE2b-256 a7280007452b4a058fb4c51cc2f7e6016416b3bcd8b64cdfc849ed3d94e45558

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