tracelog

package
v1.141.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package tracelog is the platform's log handler (#1894): JSON records to stderr, each stamped with the trace_id and span_id of the span its context carries, so a stderr line and the span of the call that wrote it are joined by one id; and, when the deployment exports log records over OTLP, the same records fanned out to the export handler at the same level.

Build assembles it. Handler is the stamp alone, for a sink of the caller's choosing. The package reads the span off the context and knows nothing about the platform, which is what makes it usable before the platform exists: the composition root installs it as the default logger's handler first, and every slog.*Context call after that is correlated.

Index

Constants

View Source
const (
	KeyTraceID = "trace_id"
	KeySpanID  = "span_id"
)

The attribute keys, the names the OpenTelemetry log data model and the collector's trace-to-log correlation read.

Variables

This section is empty.

Functions

func Build

func Build(w io.Writer, level slog.Level, export slog.Handler) slog.Handler

Build assembles the default logger's handler: a JSON handler on w at level, under the trace-id stamp, and, when export is not nil, the export handler beside it under the same level. The OTLP bridge sets the trace ids on its own records from the context, so it is not under the stamp; it is under the level filter so the export follows LOG_LEVEL as stderr does.

Types

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler is an slog.Handler that stamps trace_id and span_id onto a record logged with a context carrying a valid span, then hands it to the handler it wraps. A record logged without a span passes through unchanged.

func New

func New(next slog.Handler) *Handler

New wraps next.

func (*Handler) Enabled

func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool

Enabled defers to the wrapped handler.

func (*Handler) Handle

func (h *Handler) Handle(ctx context.Context, r slog.Record) error

Handle adds the span's ids when ctx carries one.

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs wraps the wrapped handler's WithAttrs.

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

WithGroup wraps the wrapped handler's WithGroup. The trace ids are added at Handle time, after the group is open, so they land inside it with the record's other attributes, as the slog contract for a group says.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL