Loki-compatible logging SDK for hivetrace services. Provides a unified plain key=value log format and per-service field schemas.
Project description
loki-sdk
Loki-compatible logging SDK. Plain key=value wire format, per-service typed schemas, async-safe context propagation, auto-injected exception summary. Built on stdlib logging — no structlog/loguru.
Install
pip install loki-sdk
Usage
import logging
from loki_sdk import FieldSpec, LogSchema, LoggingConfig, bound, configure
SCHEMA = LogSchema(
service_name="my-service",
fields=(FieldSpec("status_code"), FieldSpec("duration_ms")),
)
configure(LoggingConfig.from_env(SCHEMA))
logger = logging.getLogger(__name__)
with bound(request_id="abc-123", endpoint="POST /clean-text"):
logger.info("request_completed", extra={"status_code": 200, "duration_ms": 12.4})
Output:
2026-04-29T10:00:00Z [INFO] [my-service] [POST /clean-text] request_completed | status_code=200 duration_ms=12.4 request_id=abc-123
On exceptions, error="ClassName: msg" and caller=<module>:<func>:<lineno> are injected automatically.
Configuration
Read by LoggingConfig.from_env:
| Variable | Default | Effect |
|---|---|---|
LOG_LEVEL |
INFO |
Root log level. |
LOG_FORMAT |
plain |
Wire format. Only plain is supported in v0.1. |
LOG_CALLER_ON_ERROR |
1 |
Inject caller= on records with levelno >= ERROR or exc_info. |
Public API
| Symbol | Purpose |
|---|---|
LogSchema |
Per-service field declaration. |
FieldSpec |
One field in a LogSchema. |
LoggingConfig |
Args for configure(). Prefer from_env(SCHEMA). |
configure |
Wires the SDK into stdlib logging. |
bind / reset |
Token-based contextvar binding. |
bound |
Context-manager flavor of bind. |
clear |
Reset the current logging context. |
get_context |
Read-only snapshot of the current context dict. |
get_logger |
Re-export of logging.getLogger. |
License
Apache-2.0. See LICENSE.
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
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 loki_sdk-0.1.1.tar.gz.
File metadata
- Download URL: loki_sdk-0.1.1.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
827c7332eb73d8d71f6e0b31815a41675706578c723bef49a836dfce9a66e955
|
|
| MD5 |
c06c9c9454505a619ff224caf808af2e
|
|
| BLAKE2b-256 |
3fe9905ddd6137a594d1fb607e59c73b574964cc13fe809a7b36b0f469000220
|
File details
Details for the file loki_sdk-0.1.1-py3-none-any.whl.
File metadata
- Download URL: loki_sdk-0.1.1-py3-none-any.whl
- Upload date:
- Size: 16.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c066f94d74bd0fb30841ff971854f0af93f97b9215cd82028e524a09b1b37ad
|
|
| MD5 |
fec9a420ec3946c9979b2bf27b6ad5e3
|
|
| BLAKE2b-256 |
5a8d35c3fdee378b01b3d3467f2bf0f00633a51e868162228599f28b374f9527
|