Skip to main content

🚀 tjzegmott-python-logging-loki

Modern Python logging handler for Grafana Loki

PyPI version Python

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 tjzegmott-python-logging-loki

🎯 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.


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tjzegmott_python_logging_loki-0.0.1.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file tjzegmott_python_logging_loki-0.0.1.tar.gz.

File metadata

File hashes

Hashes for tjzegmott_python_logging_loki-0.0.1.tar.gz
Algorithm Hash digest
SHA256 79d19ab85dee985ea3137ce41247e4c7820ad6acdc09bbaac4043b2615f2befb
MD5 3a4ea1274d3712957b7ccc21b9707f0d
BLAKE2b-256 7a314d3a3b0085142fb768c12ba7e7d0fcde77951a1772e333c7918450086aef

See more details on using hashes here.

File details

Details for the file tjzegmott_python_logging_loki-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for tjzegmott_python_logging_loki-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1f0ea1c84f7c8d45525b412ec4804f2c77d5905b6d9124363a1a18b0aa7fb582
MD5 6ee631a68559ca214dd8b1b7a23eab71
BLAKE2b-256 0daaf6ae2680548f2b8ac001e5b475c7ab114c3ae12f8b3de0b456e3617a3681

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page