🚀 tjzegmott-python-logging-loki
Send Python logs directly to Grafana Loki with minimal configuration.
| What | Where |
|---|---|
| Source | https://github.com/tjzegmott/python-logging-loki |
| PyPI | pip install tjzegmott-python-logging-loki |
| Releases | https://github.com/tjzegmott/python-logging-loki/releases |
✨ 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
Or using uv (recommended):
uv add 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 |
suppress_errors |
bool |
False |
Silently swallow delivery failures instead of printing the standard logging --- Logging error --- traceback |
🚨 Error Handling
By default, if a log record can't be delivered to Loki (network failure, TLS error,
or an unexpected HTTP status code such as 405 Method Not Allowed), Python's standard
logging machinery prints a --- Logging error --- traceback to stderr for every failed
record (see logging.Handler.handleError).
A 405 typically means the request never reached Loki's push endpoint as a POST -
check for things like an http:// → https:// redirect, a reverse proxy/ingress that
only allows GET on that path, or a URL that doesn't end in /loki/api/v1/push. Test
with curl -X POST <url> to confirm the endpoint accepts POST requests directly.
If you'd rather not have delivery failures spam your application logs while you
investigate (or you're fine losing occasional log lines), set suppress_errors=True:
handler = logging_loki.LokiHandler(
url="https://loki.example.com/loki/api/v1/push",
tags={"app": "my-application"},
version="2",
suppress_errors=True,
)
The handler still closes/resets its HTTP session on failure so subsequent attempts start clean; it just skips the noisy stderr traceback.
🏷️ 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"}
)
Development
Prerequisites
- Python 3.8+
- uv for package management
Setup
git clone https://github.com/tjzegmott/python-logging-loki.git
cd python-logging-loki
make install
Running Tests
make test
# With coverage
make test-cov
# Across all Python versions
make test-matrix
Code Quality
# Run all checks (lint, format, type-check)
make verify
# Auto-fix lint and format issues
make fix
Prek
prek install
prek run --all-files
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgements
Based on python-logging-loki by GreyZmeem.
Metadata
Release files for tjzegmott-python-logging-loki 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| tjzegmott_python_logging_loki-0.1.0.tar.gz | 11.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tjzegmott_python_logging_loki-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 20.4 kB
Release files / tjzegmott_python_logging_loki-0.1.0.tar.gz
| Download URL | tjzegmott_python_logging_loki-0.1.0.tar.gz |
|---|---|
| Size | 11.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f15e6ecc9c8158d5dac80b62f96b4561b1a3789e9068fe7cad0da04b6ea5c44a
|
|
BLAKE2b-256 checksum How to use checksums |
15aada51f98b1d19af6ccbba733ba4875c3652363ec4be40cdb13a79cd60c21a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.
Transparency logRelease files / tjzegmott_python_logging_loki-0.1.0-py3-none-any.whl
| Download URL | tjzegmott_python_logging_loki-0.1.0-py3-none-any.whl |
|---|---|
| Size | 9.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b1d53ec964149b8c896a8e879696483351914326974bd30f500567920a5375a5
|
|
BLAKE2b-256 checksum How to use checksums |
29c1359a5a38c8d0a2020672c826b429e9fc8fbf601bd1ec6478f5b8d0b420e1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.
Transparency log