Python logging handler for Grafana Loki
Project description
🚀 python-logging-loki-v2
Modern Python logging handler for Grafana Loki
Documented by Grafana: https://github.com/grafana/loki/pull/16397
Send Python logs directly to Grafana Loki with minimal configuration.
✨ Features
- 📤 Direct Integration - Send logs straight to Loki
- 🔐 Authentication Support - Basic auth and custom headers
- 🏷️ Custom Labels - Flexible tagging system
- ⚡ Async Support - Non-blocking queue handler included
- 🔒 SSL Verification - Configurable SSL/TLS settings
- 🎯 Multi-tenant - Support for Loki multi-tenancy
📦 Installation
pip install python-logging-loki-v2
🎯 Quick Start
Basic Usage
import logging
import logging_loki
handler = logging_loki.LokiHandler(
url="https://loki.example.com/loki/api/v1/push",
tags={"app": "my-application"},
auth=("username", "password"),
version="2"
)
logger = logging.getLogger("my-app")
logger.addHandler(handler)
logger.info("Application started", extra={"tags": {"env": "production"}})
Async/Non-blocking Mode
For high-throughput applications, use the queue handler to avoid blocking:
import logging.handlers
import logging_loki
from multiprocessing import Queue
handler = logging_loki.LokiQueueHandler(
Queue(-1),
url="https://loki.example.com/loki/api/v1/push",
tags={"app": "my-application"},
version="2"
)
logger = logging.getLogger("my-app")
logger.addHandler(handler)
logger.info("Non-blocking log message")
⚙️ Configuration Options
| Parameter | Type | Default | Description |
|---|---|---|---|
url |
str |
required | Loki push endpoint URL |
tags |
dict |
{} |
Default labels for all logs |
auth |
tuple |
None |
Basic auth credentials (username, password) |
headers |
dict |
None |
Custom HTTP headers (e.g., for multi-tenancy) |
version |
str |
"1" |
Loki API version ("0", "1", or "2") |
verify_ssl |
bool |
True |
Enable/disable SSL certificate verification |
🏷️ Labels
Logs are automatically labeled with:
- severity - Log level (INFO, ERROR, etc.)
- logger - Logger name
- Custom tags - From handler and
extra={"tags": {...}}
logger.error(
"Database connection failed",
extra={"tags": {"service": "api", "region": "us-east"}}
)
🔐 Multi-tenant Setup
handler = logging_loki.LokiHandler(
url="https://loki.example.com/loki/api/v1/push",
headers={"X-Scope-OrgID": "tenant-1"},
tags={"app": "my-app"}
)
Based on python-logging-loki by GreyZmeem.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_logging_loki_v2-1.1.1.tar.gz.
File metadata
- Download URL: python_logging_loki_v2-1.1.1.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
327d539898f2b397814f8bc1b57f0bc8721a6aced802a2f8cf6fd52c0b46d208
|
|
| MD5 |
60f8042cc40935a53aa0b412955fe0bb
|
|
| BLAKE2b-256 |
e22b814681aa9857acebd5c70139a270fc560d3332b54e820b7d7cf184617724
|
File details
Details for the file python_logging_loki_v2-1.1.1-py3-none-any.whl.
File metadata
- Download URL: python_logging_loki_v2-1.1.1-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca07bbcd3245916fd222aa452cc3f9ff4fffc8ee8292a5fb743643437013b46c
|
|
| MD5 |
5c564f12191dd09b42f71cf241a7757f
|
|
| BLAKE2b-256 |
ff57cc4200ae68d141468b361235619791a37927e26e12de02a92335f302ebb9
|