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

1. 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,
        },
    },
}

2. Install Loki (if not already available)

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

3. 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!"})

➕ Adding Extra Data to Logs

You can provide additional metadata using the extra parameter:

logger.error("Failed to process request", extra={"user_id": 456, "error_code": "E500"})

🧪 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.8.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.8-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: loki_django_logger-1.0.8.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.8.tar.gz
Algorithm Hash digest
SHA256 3fd90f0506e7892ea48abd108ec09abe55c9a70f78f2d6cfe45e44791453b930
MD5 f26f837f4965dbf2dee2d1f4a4ea3237
BLAKE2b-256 116bedaa771a3745baf7c584c9b4714b5c364ac4f35704a4a78402bff4c25aad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for loki_django_logger-1.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 56f9b490ed2b97f39c36b2519ae5d0947c062ba385cc0225e272723f002369af
MD5 8664b4cbf006ff1a3c6d5e80356e7ea0
BLAKE2b-256 70169c8ea65ce9db544f157f4d01fcf0789bd37a386012c30d3228f0ee739153

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