Skip to content

Logging and telemetry adapters

Python structured logging

StructuredLoggingSink emits through the standard logging package. The complete event dictionary is available on LogRecord.agent_event, allowing JSON formatters and log handlers to serialize it without parsing a message string.

import logging
from agentlogsafe import AgentLogger, StructuredLoggingSink

logger = logging.getLogger("application.agent_audit")
log = AgentLogger(sink=StructuredLoggingSink(logger))
log.decision("request manual review", requires_human_review=True)

The stable human-readable message is agent_event; event_id, run_id, and event_type are also attached as record attributes.

OpenTelemetry

OpenTelemetrySink uses the standard tracer context-manager contract without adding an OpenTelemetry runtime dependency:

from opentelemetry import trace
from agentlogsafe import AgentLogger, OpenTelemetrySink

tracer = trace.get_tracer("my-application")
log = AgentLogger(sink=OpenTelemetrySink(tracer))

Each event creates an instantaneous agentlogsafe.<event_type> span with correlation, agent, provider, model, tool, and status attributes. Payload export is disabled by default because telemetry backends often have different access and retention rules:

OpenTelemetrySink(tracer, include_payload=True)

Only enable payload export after reviewing the destination. AgentLogger redaction still occurs before the adapter receives an event.

Note

Generative-AI semantic conventions continue to evolve. Provider and model use gen_ai.* attributes; package-specific fields use the stable agentlogsafe.* namespace so schema changes remain explicit.