Grafana Loki logging — async httpx only: LokiClient, push_log, LokiHandler.
Project description
tb-loki-central-logger
Send logs to Grafana Loki with httpx (async API for direct pushes; LokiHandler uses a background thread so logging.info stays synchronous).
Python 3.10+ · dependency: httpx · import: tb_loki_central_logger (source in app/)
Install
pip install tb-loki-central-logger
# TestPyPI: pip install -i https://test.pypi.org/simple/ tb-loki-central-logger
# From this repo: pip install -e .
Environment
Copy env.example to .env in the process working directory (loaded on first import tb_loki_central_logger).
| Variable | Purpose |
|---|---|
GRAFANA_CLOUD_URL |
Loki / stack URL |
GRAFANA_CLOUD_USER |
Basic auth user (stack id) |
GRAFANA_CLOUD_API_KEY |
Basic auth password (write token) |
Legacy: GRAFANA_CLOUD_WRITE_API_KEY if GRAFANA_CLOUD_API_KEY is unset.
Where to find GRAFANA_CLOUD_URL and GRAFANA_CLOUD_USER
- Sign in to Grafana Cloud, pick your stack in the GRAFANA CLOUD sidebar.
- On Manage stack, open the Loki card → Details or Send logs. That page shows the Loki URL (often
https://logs-prod-…grafana.netwith push path/loki/api/v1/push) — use the host or full URL forGRAFANA_CLOUD_URL. - The same screen (or the “Send logs” / client setup snippet) shows the User value for HTTP Basic auth — put that number/string in
GRAFANA_CLOUD_USER.
Quick start (setup_central_logging)
JSON on stderr and Loki when credentials exist. Use the same logger name you pass to setup_central_logging. Optional tail fields via extra_json_fields. request_id, session_id, method, path, and status are always present on each line ("-" if unset); see app/log_config.py.
import logging
from tb_loki_central_logger import setup_central_logging, shutdown_central_logging
LOGGER_NAME = "layer-gateway-llm-inference-v1"
loki_handler = setup_central_logging(
logger_name=LOGGER_NAME,
extra_json_fields=(
"retrieval_latency_s",
"rerank_latency_s",
"llm_latency_s",
),
service="my-app-test",
component="api",
env="dev",
version="1.3.0",
loki_labels={"team": "platform"},
)
_payload = {
"request_id": "request_id_1",
"session_id": "session_id_1",
"method": "POST",
"path": "/v1/rag/query",
"status": "200",
"retrieval_latency_s": None,
"rerank_latency_s": None,
"llm_latency_s": None,
}
log = logging.LoggerAdapter(logging.getLogger(LOGGER_NAME), _payload)
try:
log.info("hello info")
log.warning("hello warning")
log.error("hello error")
log.extra["retrieval_latency_s"] = 1.234
log.extra["rerank_latency_s"] = 2.345
log.extra["llm_latency_s"] = 4.567
log.info("hello info with timing")
finally:
shutdown_central_logging(LOGGER_NAME, loki_handler)
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 tb_loki_central_logger-0.2.6.tar.gz.
File metadata
- Download URL: tb_loki_central_logger-0.2.6.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b6a0c24275707881e9eabf7334e322422a5d7f4c38b460806b88c8ba0d857594
|
|
| MD5 |
5ab503469a8dd343fa92ce241afb825f
|
|
| BLAKE2b-256 |
e81240ac18de0f9078216ec1c8ecb3e28433bed33b535a918b9031ee46b84d62
|
File details
Details for the file tb_loki_central_logger-0.2.6-py3-none-any.whl.
File metadata
- Download URL: tb_loki_central_logger-0.2.6-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8fb02bc43d9df9ed9a90d413174f75217afff49eb3507d2b2bb7a7040fd0d5a3
|
|
| MD5 |
9bf122e026ccfa062db659d1be65724c
|
|
| BLAKE2b-256 |
c1c4c55fa55dd06893a9862567c13c9f9af47f6a8ca57c714dd7d6bec48e9761
|