Skip to main content

A python loggin library for Grafana Loki

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xogywlt_loki_logger-1.0.4.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xogywlt_loki_logger-1.0.4-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file xogywlt_loki_logger-1.0.4.tar.gz.

File metadata

  • Download URL: xogywlt_loki_logger-1.0.4.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.9

File hashes

Hashes for xogywlt_loki_logger-1.0.4.tar.gz
Algorithm Hash digest
SHA256 e849e997ee5d026e10db9b529b6dbb649030b168fc67f2d630767ed3e513d19d
MD5 1dc603db1b2c256f446a2a3b3f07a701
BLAKE2b-256 6e4d15fbd9c19de5df30cc492d6dc3c679a87095027fef3fd38db60ed680197f

See more details on using hashes here.

File details

Details for the file xogywlt_loki_logger-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for xogywlt_loki_logger-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 217177589a148b23014b91d98ba0be6c9ea517e0cd1d3fe64534ac7df70fdf34
MD5 df8f7bd52e089d357b6fd4b38fae0da2
BLAKE2b-256 5180165b8ff07f86d8d34ea9a74eaa0ead4397d2b5c0848079e5df498934b458

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page