Skip to main content

AgentLog — Python

Language-neutral, structured logging framework for Python. Emits Schema v1 JSONL conforming to docs/schema/log-event.schema.json — identical to the Kotlin (agent-log-android), Swift (agent-log-macos), TypeScript (agent-log-web) and Java/Go siblings.

Zero dependencies. Standard library only. The wire-format serializer is hand-rolled, so the top-level key set is guaranteed closed (additionalProperties: false).

Quickstart

from agentlog import AgentLog, LoggingConfig

# Bootstrap once (defaults: service="app", level="info", redact on, export to <tmp>/agent-log)
AgentLog.bootstrap(config_json='{"service": "my-app", "level": "debug"}')

AgentLog.info("hello world", payload={"host": "example.com"})
AgentLog.warn("low disk", category="system", payload={"free_mb": 128})
AgentLog.error("boom", payload={"password": "hunter2"})  # password auto-masked

# Child logger — merges scope, never mutates the parent
child = AgentLog.child({"user_id": "u-1", "subsystem": "billing"})
child.info("invoice created", payload={"invoice_id": "INV-9"})

# Append-only redaction paths
AgentLog.redact(["my_secret"])

print("Export:", AgentLog.export_path())

API

Module-level facade (agentlog):

Method Description
bootstrap(config=None, config_json=None, sink=None) Initialize the singleton logger.
info/warn/error/debug/trace/critical(message, category="app", trace_id=None, payload=None) Manual log (agent_auto=False).
record_auto(event_type, message, category="auto", trace_id=None, payload=None) Auto-instrumented log (agent_auto=True, always written).
child(context) New logger merging scope fields (parent unchanged).
set_level(level) Set minimum output level.
redact(paths) Append sensitive key paths (defaults can't be removed).
export_path() Path of the emitted JSONL file.

Levels

trace | debug | info | warn | error | critical (6, lowercase). warning is a read-only alias that is never written — it normalizes to warn on read.

Wire format (Schema v1.3)

Required top-level keys: ts, level, service, event_type, session_id, agent_auto, message. Optional: subsystem, category, env, release, user_id, trace_id, span_id, payload. No other keys allowed.

release (v1.3) is a config-level identifier (LoggingConfig(release="1.4.2") or {"release": "1.4.2"} in the config JSON) stamped on every event by the logger — semver, git sha or build number, ≤128 chars, no format validation. Omit it and the key is not written; when present it becomes service.version in the OTLP resource.

Crash / hang payloads follow the v1.3 vocabulary: exception (type name), exception_message, stack (single string) and mechanism (uncaught | handled | reported). Manual logs use event_type="log"; auto events use network_request | db_query | crash | hang | screen_view | ui_interaction.

span_id (v1.1) is 16 lowercase hex chars (8 bytes), the same shape as the W3C traceparent span-id. Bind it on a child logger so every event emitted inside an operation shares one span — and, when exported to OTLP, one spanId:

span = AgentLog.child({"trace_id": "0123…", "span_id": "a1b2c3d4e5f60718"})
span.info("invoice generated")              # -> span_id: a1b2c3d4e5f60718
span.record_auto("network_request", "GET /orders -> 200", payload={...})

A malformed span_id is dropped (never written), so a bad value can't produce a schema-violating line; exporters then derive one deterministically.

Configuration (LoggingConfig.from_json)

Key Default Notes
service "app" Required, non-empty.
env — dev/staging/prod.
level "info" Min level (write side; warning→warn).
redact_enabled true Master toggle.
redact_paths [] Append to default sensitive keys.
export_directory <tmp>/agent-log Output dir for agent-log.jsonl.

Automatic instrumentation

Two opt-in helpers are provided (the package stays standard-library only — requests is a soft dependency, so instrument_requests is a no-op when it is not installed):

from agentlog import install_crash_handler, instrument_requests

install_crash_handler()   # sys.excepthook -> record_auto("crash", ...) with stack
instrument_requests()     # monkey-patch requests.Session.send -> network_request
  • install_crash_handler() — installs a sys.excepthook that records uncaught exceptions as crash auto-events (with thread / exception / stack); uninstall_crash_handler() restores the previous hook.
  • instrument_requests() — patches requests.Session.send (idempotent) so every HTTP call emits a network_request auto-event. For clients other than requests (e.g. urllib), call record_network_request(...) directly.

Crash handlers, signal handlers, or a context-manager can be plugged in; any of them should call record_auto(...) so the event carries agent_auto: true.

OTLP export (OpenTelemetry)

agentlog.otlp translates already-emitted Schema v1 events into OTLP/JSON, so they can be shipped to any OpenTelemetry-compatible backend (Jaeger, Tempo, Loki, Grafana, Datadog …) without changing the wire format.

from agentlog import OTLPSink, to_otlp_json
from agentlog.otlp import to_otlp

sink = OTLPSink(extra_resource={"service.version": "1.4.0"})
AgentLog.bootstrap(config=config, sink=sink)

AgentLog.info("checkout started", payload={"cart_size": 3})
record_network_request("GET", "https://api.example.com/orders", 200, 14)  # -> CLIENT span

batch = sink.drain()                       # or: to_otlp(jsonl_lines)
logs_body = to_otlp_json(batch["logs"])    # POST to <collector>/v1/logs
traces_body = to_otlp_json(batch["traces"])# POST to <collector>/v1/traces

The posting itself is one call — agentlog.otlp_http.OTLPExporter handles one-signal-per-request, skipping empty signals, and the OTLP retry policy:

from agentlog import OTLPExporter

exporter = OTLPExporter("http://localhost:4318")   # OTLP/HTTP default
exporter.export(batch)                             # or exporter.export_sink(sink)

2xx succeeds; 408/429/5xx and transport errors retry with exponential backoff (100 ms base, doubling, capped at 64×); other 4xx fail fast because a malformed or unauthorised payload will not fix itself. Defaults: 2 retries, 10 s timeout.

The exporter is a transport, not a queue (no batching timer, no disk buffer, no backpressure). In production point it at a local Collector.

Mapping contract (details in docs/logging-framework/OTLP-MAPPING.md):

agent-log OTLP
every event one LogRecord
level severityNumber 1/5/9/13/17/21 (critical → FATAL)
service / env / session_id resource service.name / deployment.environment / session.id
trace_id traceId (32-hex; opaque values hashed deterministically)
span_id spanId (used verbatim when valid; derived only when absent)
event_type / agent_auto / subsystem / category attributes agent.event.type / agent.auto / agent.subsystem / agent.category
user_id enduser.id
payload.* payload.<key> attributes (ints as strings per proto3 JSON)
network_request + SPAN_KIND_CLIENT span (http.request.method, url.full, http.response.status_code)
db_query + SPAN_KIND_CLIENT span (db.system, db.operation, db.statement)
crash / hang LogRecord + exception.type / exception.message / exception.stacktrace

Ids are deterministic: an event's own span_id (Schema v1.1) is used verbatim; otherwise ids are SHA-256 derived, so re-exporting the same event yields the same traceId/spanId and retries stay idempotent.

AI interaction events (Schema v1.4)

All six ends share one AI event contract (INTERFACE.md §4.4): six standard event_type values, a snake_case payload vocabulary, and agent_auto=false (explicit business-side instrumentation — the framework never hooks an LLM SDK).

from agentlog import record_ai_request, record_ai_response, record_ai_error

record_ai_request("gpt-4o", "summarize this", request_id="req_001", provider="openai")
record_ai_response("gpt-4o", 530, request_id="req_001",
                   prompt_tokens=320, completion_tokens=210, finish_reason="stop")
record_ai_error("RateLimitError", "429 too many requests", request_id="req_001")
helper event_type level
record_ai_request ai_request_initiated info
record_ai_response ai_response_received info
record_ai_tool_call ai_tool_call_initiated info
record_ai_tool_result ai_tool_call_completed info
record_ai_guardrail ai_guardrail_triggered warn
record_ai_error ai_error error

request_id links request → response → tool → error within one turn; it is optional and never generated/validated by the framework. Extra keyword arguments are forwarded into payload verbatim — use the standard snake_case keys (model, prompt_text, total_tokens, tool_name, ok, guardrail_type, exception, ...). ai_error reuses the v1.3 exception vocabulary. OTLP export emits a LogRecord only — no span; attach span_id explicitly if a downstream system needs to build one.

Run tests

python3 -m unittest discover -s tests -v

All tests use the standard library (unittest); no pytest required.

Repository layout

agentlog/   core implementation (stdlib only)
tests/      unittest suites incl. C7 SchemaTests
docs/       INTERFACE.md + Schema v1 (copied verbatim from ai-dev-spec)
example/    tiny runnable demo

Metadata

Release files for agentlog-py 1.4.0

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

Source distribution (sdist)

Source distribution for agentlog-py 1.4.0
File Size Uploaded Provenance Actions
agentlog_py-1.4.0.tar.gz 47.4 kB Without provenance Details for agentlog_py-1.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for agentlog-py 1.4.0
File Interpreter ABI Platform Actions
agentlog_py-1.4.0-py3-none-any.whl Python 3 none any Details for agentlog_py-1.4.0-py3-none-any.whl

Total release size: 82.1 kB

Release files / agentlog_py-1.4.0.tar.gz

Download URL agentlog_py-1.4.0.tar.gz
Size 47.4 kB
Tags Source
SHA-256 checksum
How to use checksums
10e9aae25469ceabf995f8d9a4c6ac7fbbd2d9fb3421e6e7cc8b22fc6642cdea
BLAKE2b-256 checksum
How to use checksums
af4a343064a8c8766a947fb35a50e6c013bbac9c8b9de77831c716dbeefb7770
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.12

Release files / agentlog_py-1.4.0-py3-none-any.whl

Download URL agentlog_py-1.4.0-py3-none-any.whl
Size 34.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
277c6e4e1fc43c22b3bdf6c9f9a042d8e762b0c59f78d6b85a8b0d29454530c6
BLAKE2b-256 checksum
How to use checksums
fb3bd16d05dd5b0f44e2a903b3e3ce09744f6480b69deaa99bd05c4db19f5c89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.12

Security for agentlog-py 1.4.0

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.

Release history Release notifications | RSS feed

This release

1.4.0 This release

2 release files No provenance

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