Skip to main content

Structured logging for Python with optional OpenTelemetry (OTLP) export to providers like Seq

Project description

roz-logs

Structured logging for Python with optional OpenTelemetry (OTLP) export to providers like Seq.

roz-logs is a thin, dependency-free wrapper over the standard library logging module. It gives you:

  • Structured fields — pass key=value pairs to any log call, rendered as readable key=value text or as JSON.
  • Context bindinglog.bind(device="pi-01") returns a child logger that stamps every record with that context.
  • One-call setupconfigure() installs handlers/formatters and reads sensible defaults from environment variables.
  • Opt-in cloud export — set ROZ_LOGS_OTLP_ENDPOINT to ship logs to any OTLP/HTTP collector (Seq, Grafana, an OpenTelemetry Collector, …). With no endpoint set, it logs to the console and has zero heavy dependencies — ideal for offline/embedded use such as the Raspberry Pi card sorter.

Install

pip install roz-logs            # console logging, no extra deps
pip install "roz-logs[otlp]"    # + OpenTelemetry OTLP export

Usage

from roz_logs import configure, get_logger

configure(service_name="card-sorter")        # call once at startup
log = get_logger(__name__)

log.info("sorted card", card="Black Lotus", bin=3)
# 2026-06-24T19:40:00+00:00 INFO     [card-sorter] __main__: sorted card card="Black Lotus" bin=3

job = log.bind(job_id="abc123")               # bound context
job.warning("reject bin full", bin=9)

try:
    risky()
except Exception:
    log.exception("operation failed", op="sort")   # includes traceback

JSON output (great for log shippers):

configure(service_name="card-sorter", json_output=True)
# {"timestamp": "...", "level": "INFO", "service": "card-sorter", "logger": "...", "message": "sorted card", "card": "Black Lotus", "bin": 3}

Configuration

Every configure() argument falls back to an environment variable, so you can deploy without touching code:

Argument Environment variable Default
service_name ROZ_LOGS_SERVICE_NAME app
level ROZ_LOGS_LEVEL INFO
json_output ROZ_LOGS_JSON false
otlp_endpoint ROZ_LOGS_OTLP_ENDPOINT (unset → console only)
otlp_headers ROZ_LOGS_OTLP_HEADERS (unset)

ROZ_LOGS_JSON accepts any of 1, true, yes, on (case-insensitive) to switch from the human-readable TextFormatter to the line-delimited JsonFormatter; anything else keeps text output.

OTLP export (shipping to Seq and other collectors)

The core library has zero runtime dependencies and only ever writes to the console. Cloud/collector export is opt-in through the otlp extra, which pulls in the OpenTelemetry SDK and the OTLP/HTTP log exporter:

pip install "roz-logs[otlp]"

Once installed, setting an OTLP endpoint makes configure() attach a second handler (in addition to the console) that batches log records and exports them over OTLP/HTTP:

export ROZ_LOGS_OTLP_ENDPOINT="http://localhost:5341/ingest/otlp/v1/logs"
export ROZ_LOGS_OTLP_HEADERS="X-Seq-ApiKey=<your-api-key>"

Under the hood build_otlp_handler() wires up an OpenTelemetry LoggerProvider (tagged with service.name = your service_name), a BatchLogRecordProcessor, and an OTLPLogExporter pointed at your endpoint, then returns a stdlib logging.Handler bridging the two. Records flow:

log.info(...) → stdlib logging → OTLP LoggingHandler → BatchLogRecordProcessor
             → OTLPLogExporter (HTTP) → Seq / OTel Collector / Grafana / …

For Seq the endpoint is http://<host>:5341/ingest/otlp/v1/logs and the API key travels in a header (X-Seq-ApiKey). Any OTLP/HTTP logs endpoint works the same way.

Graceful degradation: if you request an endpoint but the otlp extra is not installed, roz-logs logs a warning and keeps console logging working rather than crashing — so the same code runs on a constrained device (console only) and a server (console + OTLP) with no changes.

Development

pip install pytest pytest-cov
pytest                        # runs unit tests with a 90% coverage gate

# to exercise the OTLP code paths, install the extra into your test env:
pip install opentelemetry-sdk opentelemetry-exporter-otlp-proto-http

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

roz_logs-0.1.0.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

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

roz_logs-0.1.0-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file roz_logs-0.1.0.tar.gz.

File metadata

  • Download URL: roz_logs-0.1.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"25.04","id":"plucky","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for roz_logs-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b2b25e5ace8587b158ff854040a15cde3a04e42a193c39255f082879bc4ea74e
MD5 17bbc0a5491baf403cf0778d8435dbac
BLAKE2b-256 97bc08a3361314670063e881cb716ca60f558d40706546d78bf95d836de98d75

See more details on using hashes here.

File details

Details for the file roz_logs-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: roz_logs-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"25.04","id":"plucky","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for roz_logs-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0c95e4ab25fd9fd543241fe9b2b5d5a1e1ef586049a67567862afbe6b64a9fa5
MD5 09f3ea431ac30e824dd10ab8c1f9e1b1
BLAKE2b-256 d8bf0b7ca71322175c941d013b70feac87daeb98d96a348eab502eefd0bda6ec

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