Skip to main content

Loki logger for Django applications

Project description

Loki Django Logger

This package provides a lightweight logging solution for Django applications that sends logs to Grafana Loki with gzip compression for improved performance.

Installation

pip install loki-django-logger

Configuration

  1. Add the logger to your Django settings.

In your settings.py:

from loki_django_logger.logger import configure_logger

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,
            "extra": {}  # Add additional metadata here
        },
    },
}
  1. Install Loki if not already available:
docker run -d --name=loki -p 3100:3100 grafana/loki:latest
  1. 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 to your logs 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.6.tar.gz (4.9 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.6-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: loki_django_logger-1.0.6.tar.gz
  • Upload date:
  • Size: 4.9 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.6.tar.gz
Algorithm Hash digest
SHA256 c27ec5780a8680ecea476fbde685217060650690c18d5d928bf605a7209e32e4
MD5 6c70e6e569c145dbc9c72f5c00723173
BLAKE2b-256 7393c75ceb114523ff5879dbd67856adc8a431d0eb72dd1082c0f4af649d0199

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for loki_django_logger-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 ecd24ea237a7a4c44a466c0756a30917bb50bc8557375a6d3b542792ad58a676
MD5 2030e89e3b92fe697d1e8632a67b8f75
BLAKE2b-256 a2a3da68bec01e6ffcf49090a4bba478e4845af6f1cd67d0f82aab2d93a9ac93

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