Skip to main content
Application logs are in public preview and can change before reaching general availability.
Requires Python SDK v0.42.0 or later, and BRAINTRUST_API_KEY set to a Braintrust API key.
Use the Python SDK to send application logs to Braintrust. Emit messages directly from a project logger, or forward the records your code already writes with the standard-library logging module.

Emit logs from the SDK

To add log statements to your code, initialize a project logger and call a severity method: trace(), debug(), info(), warn(), error(), or fatal(). The trace() method emits a message at the trace severity level. It doesn’t start a Braintrust trace.
Each call creates one row with span_attributes.type set to log: The body can be any JSON-serializable value, not just a string.

Preserve message templates

Pass named parameters to group messages by a stable template while retaining each event’s values:
The record stores the rendered message, template, and parameters:
Missing parameters remain as literal placeholders. Malformed braces or unsupported format specifiers leave the original message body unchanged. Formatting problems don’t raise exceptions. Because the template stays the same across events, you can group by it to find your most frequent messages with SQL:
Most frequent message templates

Use t-string templates

On Python 3.14 and later, severity methods accept t-strings with embedded interpolation values. Passing separate named parameters with a t-string raises TypeError.
This produces the same rendered message, template, and parameters as the named-parameter form.

Capture existing Python logs

Attach BraintrustLogHandler to a standard-library logger to forward its records without changing existing log calls. Set the logger’s level to include the messages you want to capture:
The handler preserves the formatted message (including exception information), original timestamp, template parameters, and extra fields. It also records the logger name and source location. To prevent recursive logging, the handler ignores records from the braintrust logger and records emitted while the SDK’s HTTP transport is active.

Associate logs with a trace

Emit a message inside an active Braintrust span to associate it with that operation. For example, log a retry inside the payment operation that triggered it:
The message has its own row ID and shares the active span’s span and trace IDs. Logs can also correlate with active OpenTelemetry spans when you configure OpenTelemetry compatibility mode. Without an active span, each SDK logger instance groups its messages under a shared trace ID. Those messages can belong to different requests. Use an active span to associate messages with a specific request or operation.

Next steps