logging
robotsix_llmio.logging
Shared, stdlib-only logging setup with OTel trace-id injection.
robotsix_llmio owns the OpenTelemetry→Langfuse export layer, so the
bridge that stamps the active trace id onto log records belongs here too.
This module provides a small, idempotent :func:setup_logging helper that
both robotsix_llmio and its downstream consumers can share instead of
reinventing the same boilerplate.
For consumers that emit structured logs, :func:setup_structlog offers a
structlog-capable mode: it wires a stdlib ProcessorFormatter bridge onto
the root logger (so structlog-native and foreign stdlib records share one
processor chain and one JSON/console renderer), stamps the active OTel trace
id via :func:add_otel_trace_id, and optionally merges a contextvars-scoped
correlation id bound through :func:bind_correlation_id.
The module imports cleanly even when the tracing extra (OpenTelemetry)
is not installed: the trace id is obtained via
:func:robotsix_llmio.core.get_recording_span, which guards its own
ImportError and returns None when OpenTelemetry is absent.
filters: ["!^_"] keeps underscore-prefixed names private in the docs.
OTelTraceFilter
Bases: Filter
Stamp the active OpenTelemetry trace id onto every log record.
The filter sets record.trace_id to the 32-hex-char id of the active
recording span, or to "-" when no span is active (or OpenTelemetry is
not installed). It always returns True and never raises.
filter
filter(record)
Set record.trace_id and keep the record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
The log record to annotate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Always |
setup_logging
setup_logging(
*,
level=None,
fmt=None,
loggers=("robotsix_llmio",),
stream=None,
)
Configure application logging with OTel trace-id injection (idempotent).
Attaches a single :class:logging.StreamHandler — carrying an
:class:OTelTraceFilter and the resolved formatter — to each named
logger, and sets each logger's level. The root logger is never touched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
int | str | None
|
Explicit level (name or int). Falls back to |
None
|
fmt
|
str | None
|
|
None
|
loggers
|
Sequence[str]
|
Logger names to configure. Defaults to llmio's own namespace; consumers pass their app namespaces too. |
('robotsix_llmio',)
|
stream
|
TextIO | None
|
Target stream. Defaults to :data: |
None
|
Returns:
| Type | Description |
|---|---|
None
|
|
add_otel_trace_id
add_otel_trace_id(_logger, _method_name, event_dict)
structlog processor that stamps the active OTel trace id onto the event.
Mirrors :class:OTelTraceFilter for the structlog pipeline: sets
event_dict["trace_id"] to the 32-hex-char id of the active recording
span, or to "-" when no span is active (or OpenTelemetry is absent). It
is duck-typed and never raises, so a partial span shim cannot break logging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
_logger
|
WrappedLogger
|
The wrapped logger (unused). |
required |
_method_name
|
str
|
The called log method name (unused). |
required |
event_dict
|
EventDict
|
The structlog event dict to annotate. |
required |
Returns:
| Type | Description |
|---|---|
EventDict
|
The same |
bind_correlation_id
bind_correlation_id(correlation_id)
Bind (or clear) a correlation id on the current structlog context.
When setup_structlog(correlation_id=True) is active, the bound value is
merged onto every subsequent structured log event via
:func:structlog.contextvars.merge_contextvars. The binding is
contextvars-scoped, so it is safe under asyncio and threads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
correlation_id
|
str | None
|
The id to bind, or |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
setup_structlog
setup_structlog(
*,
level=None,
fmt=None,
loggers=("robotsix_llmio",),
stream=None,
correlation_id=False,
)
Configure structlog + stdlib logging via a root ProcessorFormatter bridge.
This is the structlog-capable counterpart to :func:setup_logging. It wires
a single :class:structlog.stdlib.ProcessorFormatter handler onto the root
logger so that both structlog-native calls (structlog.get_logger) and
foreign stdlib logging records render through one shared processor chain
and one renderer. The call is idempotent: repeat calls reuse the root
handler instead of stacking duplicates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
int | str | None
|
Explicit level (name or int). Falls back to |
None
|
fmt
|
str | None
|
|
None
|
loggers
|
Sequence[str]
|
Logger names whose level is set and whose records are allowed to
propagate to the root bridge. Any handler previously attached by
:func: |
('robotsix_llmio',)
|
stream
|
TextIO | None
|
Target stream. Defaults to :data: |
None
|
correlation_id
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
None
|
|