loki-reader-core
A lightweight Python library for querying Grafana Loki logs via REST API.
Features
- Simple, intuitive client for Loki's HTTP API
- Supports
query,query_range, label discovery, and series matching - SSL/TLS support including custom CA certificates for self-signed certs
- Multi-tenant support via
X-Scope-OrgIDheader - Basic authentication
- Clean dataclass models with
to_dict()/from_dict()serialization - All timestamps as Unix nanoseconds (Loki's native format)
Installation
From PyPI:
pip install loki-reader-core
From GitHub:
pip install loki-reader-core @ git+ssh://git@github.com/jmazzahacks/loki-reader-core.git@main
Usage
Basic Query
from loki_reader_core import LokiClient
from loki_reader_core.utils import hours_ago_ns, now_ns
client = LokiClient(base_url="https://loki.example.com")
result = client.query_range(
logql='{job="api-server"} |= "error"',
start=hours_ago_ns(1),
end=now_ns(),
limit=500
)
for stream in result.streams:
print(f"Labels: {stream.labels}")
for entry in stream.entries:
print(f" [{entry.timestamp}] {entry.message}")
With Authentication and Self-Signed Certificates
client = LokiClient(
base_url="https://loki.internal.company.com:8443",
auth=("username", "password"),
ca_cert="/path/to/ca.pem"
)
Multi-Tenant Setup
client = LokiClient(
base_url="https://loki.example.com",
org_id="tenant-1"
)
Exploring Labels
labels = client.get_labels()
values = client.get_label_values("application")
series = client.get_series(match=['{application="my-app"}'])
Context Manager
with LokiClient(base_url="https://loki.example.com") as client:
result = client.query(logql='{job="api"}')
API Reference
LokiClient
| Parameter | Type | Default | Description |
|---|---|---|---|
base_url |
str |
required | Loki server URL |
auth |
tuple[str, str] |
None |
Basic auth (username, password) |
org_id |
str |
None |
X-Scope-OrgID for multi-tenant |
ca_cert |
str |
None |
Path to CA certificate PEM file |
verify_ssl |
bool |
True |
Set False to disable SSL verification |
timeout |
int |
30 |
Request timeout in seconds |
Methods
| Method | Description |
|---|---|
query(logql, time, limit) |
Instant query at a single point in time |
query_range(logql, start, end, limit, direction) |
Query across a time range |
get_labels(start, end) |
List available label names |
get_label_values(label, start, end) |
List values for a specific label |
get_series(match, start, end) |
List streams matching selectors |
Timestamp Utilities
from loki_reader_core.utils import (
now_ns, # Current time as nanoseconds
seconds_to_ns, # Convert Unix seconds to nanoseconds
ns_to_seconds, # Convert nanoseconds to Unix seconds
minutes_ago_ns, # Timestamp N minutes ago
hours_ago_ns, # Timestamp N hours ago
days_ago_ns, # Timestamp N days ago
)
Development
Setup
# Create virtual environment
python -m venv .
# Activate virtual environment
source bin/activate
# Install dependencies
pip install -r dev-requirements.txt
pip install -e .
Running Tests
source bin/activate
pytest tests/ -v
License
MIT
Author
Jason Byteforge (@jmazzahacks)
Metadata
Release files for loki-reader-core 0.2.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| loki_reader_core-0.2.5.tar.gz | 18.1 kB | Without provenance | Details for loki_reader_core-0.2.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| loki_reader_core-0.2.5-py3-none-any.whl | Python 3 | none | any | Details for loki_reader_core-0.2.5-py3-none-any.whl |
Total release size: 33.1 kB
Release files / loki_reader_core-0.2.5.tar.gz
| Download URL | loki_reader_core-0.2.5.tar.gz |
|---|---|
| Size | 18.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fc570fc41bead9da2a7b35c4a631192436b35a5df144203f0291754006ad81d1
|
|
BLAKE2b-256 checksum How to use checksums |
5773bc683265b6d3866fa09515ec6acc8c15bb592eb15b0260dcaf941850d546
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|
Release files / loki_reader_core-0.2.5-py3-none-any.whl
| Download URL | loki_reader_core-0.2.5-py3-none-any.whl |
|---|---|
| Size | 15.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3d42cf38dbe51d31623bb6698845e9b0de34299f6465df24c3f741a14032d142
|
|
BLAKE2b-256 checksum How to use checksums |
75ab77a09e70e206ba0ceb4521606bc4ca04d2ca54e122fe92c772f2c2786322
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|
Security for loki-reader-core 0.2.5
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.