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
- 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
},
},
}
- 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!"})
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file loki_django_logger-1.0.7.tar.gz.
File metadata
- Download URL: loki_django_logger-1.0.7.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a88850cc61f56f17fb9b6200367c6405c12098e02b02057cb4d0804ced6c51ff
|
|
| MD5 |
dc15f20903a48784ff616d3857d6f7f9
|
|
| BLAKE2b-256 |
bc173dd32976a61a1d4522e10045fd170921807327f630a303413958611abb1b
|