Skip to main content

loki_logger

A Python logging library for Grafana Loki with a clean class hierarchy, multiple handlers, structured context, and HTTP/async push support.


Installation

# No external dependencies required for synchronous usage.
pip install loki_logger          # from PyPI (when published)

# For async support:
pip install aiohttp

Quick Start

from loki_logger import configure, get_logger, LokiHandler, LogLevel

configure(
    level=LogLevel.INFO,
    handlers=[
        LokiHandler(
            url="http://localhost:3100",
            labels={"app": "my-service", "env": "production"},
        )
    ],
    default_labels={"region": "us-east-1"},
)

logger = get_logger("myapp")
logger.info("Server started", port=8080)
logger.error("Database timeout", exc_info=True, db_host="pg01")

Architecture

loki_logger/
├── levels.py        LogLevel enum  (DEBUG / INFO / WARNING / ERROR / CRITICAL)
├── record.py        LogRecord dataclass
├── formatters.py    BaseFormatter, JSONFormatter, TextFormatter, LabelFormatter
├── handlers.py      BaseHandler, ConsoleHandler, LokiHandler,
│                    AsyncLokiHandler, BufferedLokiHandler
├── logger.py        Logger class + global registry (get_logger / configure)
├── context.py       Thread-local context binding (LogContext, bind_context, …)
└── middleware.py    WSGI / ASGI HTTP logging middleware

Handlers

ConsoleHandler

Writes formatted lines to stdout (DEBUG/INFO) or stderr (WARNING+).

from loki_logger import ConsoleHandler, TextFormatter

handler = ConsoleHandler(formatter=TextFormatter())

LokiHandler

Synchronous HTTP POST to /loki/api/v1/push per log record.

from loki_logger import LokiHandler

handler = LokiHandler(
    url="http://localhost:3100",
    labels={"app": "api"},
    timeout=5.0,
    username="admin",   # optional — for Grafana Cloud
    password="secret",
)

BufferedLokiHandler

Batches records and flushes them periodically or when the buffer is full.

from loki_logger import BufferedLokiHandler

handler = BufferedLokiHandler(
    url="http://localhost:3100",
    labels={"app": "api"},
    max_batch_size=100,    # flush when 100 records accumulate
    flush_interval=5.0,    # also flush every 5 seconds
)

# Always close cleanly on shutdown:
handler.close()

AsyncLokiHandler

For asyncio applications. Schedules pushes as tasks on the running event loop.

import asyncio
from loki_logger import AsyncLokiHandler, Logger

handler = AsyncLokiHandler(url="http://localhost:3100")
logger = Logger("myapp", handlers=[handler])

async def main():
    logger.info("Async event", user_id=42)
    await handler.flush()   # wait for all pending pushes

asyncio.run(main())

Formatters

Class Output style
JSONFormatter Single-line JSON object
TextFormatter Configurable human-readable text
LabelFormatter key=value pairs (great for LogQL)
from loki_logger import JSONFormatter, TextFormatter, LabelFormatter

# JSON (default)
JSONFormatter(indent=None)

# Text with custom template
TextFormatter(template="[{timestamp}] {level}: {message} {extra}")

# Key=value pairs
LabelFormatter()

Structured Context

Attach fields automatically to all records created on the current thread:

from loki_logger import bind_context, clear_context, LogContext, get_logger

logger = get_logger()

# Manual bind / clear
bind_context(request_id="req-123", user_id=42)
logger.info("Processing")   # ← includes request_id & user_id
clear_context()

# Context manager (recommended)
with LogContext(trace_id="abc", tenant="acme"):
    logger.info("Inside block")   # ← includes trace_id & tenant
# context is automatically restored here

Child Loggers

from loki_logger import get_logger

root = get_logger("myapp")
db   = root.get_child("db")        # name = "myapp.db"
http = root.get_child("http")      # name = "myapp.http"

db.info("Query executed", query_ms=12)

HTTP Middleware

WSGI (Flask / Django)

from loki_logger import LoggingMiddleware, get_logger

app.wsgi_app = LoggingMiddleware(
    app.wsgi_app,
    logger=get_logger("http"),
    exclude_paths={"/healthz", "/metrics"},
)

ASGI (FastAPI / Starlette)

from loki_logger import LoggingMiddleware, get_logger

app.add_middleware(
    LoggingMiddleware,
    logger=get_logger("http"),
    exclude_paths={"/healthz"},
)

Each request emits a record with: method, path, status_code, duration_ms, remote_addr.


Loki Payload Format

Records are grouped by label set and pushed as Loki streams:

{
  "streams": [
    {
      "stream": { "app": "api", "env": "prod", "level": "info" },
      "values": [
        ["1700000000000000000", "{\"level\":\"INFO\",\"message\":\"Hello\"}"]
      ]
    }
  ]
}

API Reference (Summary)

# Global logger registry
logger = get_logger("myapp")            # create or retrieve
configure(level, handlers, ...)        # configure root logger

# Logger instance
logger.debug / info / warning / error / critical / exception(msg, **extra)
logger.add_handler(handler)
logger.remove_handler(handler)
logger.get_child("suffix")

# LogLevel
LogLevel.DEBUG / INFO / WARNING / ERROR / CRITICAL
LogLevel.from_string("info")

# LogRecord
record.timestamp_ns          # nanoseconds (required by Loki)
record.as_dict()             # dict representation
record.merged_labels(defaults)

# Context
bind_context(**fields)
clear_context()
get_context() -> dict
LogContext(**fields)         # context manager

License

MIT

Metadata

Release files for xOgYwlT-loki-logger 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xOgYwlT-loki-logger 1.0.4
File Size Uploaded Provenance Actions
xogywlt_loki_logger-1.0.4.tar.gz 15.6 kB Without provenance Details for xogywlt_loki_logger-1.0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for xOgYwlT-loki-logger 1.0.4
File Interpreter ABI Platform Actions
xogywlt_loki_logger-1.0.4-py3-none-any.whl Python 3 none any Details for xogywlt_loki_logger-1.0.4-py3-none-any.whl

Total release size: 32.1 kB

Release files / xogywlt_loki_logger-1.0.4.tar.gz

Download URL xogywlt_loki_logger-1.0.4.tar.gz
Size 15.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e849e997ee5d026e10db9b529b6dbb649030b168fc67f2d630767ed3e513d19d
BLAKE2b-256 checksum
How to use checksums
6e4d15fbd9c19de5df30cc492d6dc3c679a87095027fef3fd38db60ed680197f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.9

Release files / xogywlt_loki_logger-1.0.4-py3-none-any.whl

Download URL xogywlt_loki_logger-1.0.4-py3-none-any.whl
Size 16.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
217177589a148b23014b91d98ba0be6c9ea517e0cd1d3fe64534ac7df70fdf34
BLAKE2b-256 checksum
How to use checksums
5180165b8ff07f86d8d34ea9a74eaa0ead4397d2b5c0848079e5df498934b458
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.9

Security for xOgYwlT-loki-logger 1.0.4

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.

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