Requires Python SDK v0.42.0 or later, and
BRAINTRUST_API_KEY set to a Braintrust API key.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.
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: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 raisesTypeError.
Capture existing Python logs
AttachBraintrustLogHandler 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:
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:Next steps
- Inspect logs in the UI or with SQL.
- Share context with OpenTelemetry to correlate logs with active OpenTelemetry spans.
- Trace application logic to time operations and nest logs under them.