log/slog handler attaching OpenTelemetry trace details to logs.
go get github.com/go-slog/otelslog
Wrap an existing handler:
import(
"log/slog"
"github.com/go-slog/otelslog"
)
var handler slog.Handler
// Set up your handler
// handler = ...
// Wrap Handler
handler = otelslog.NewHandler(handler)
logger := slog.New(handler)
// Call logger with a context
logger.InfoContext(ctx, "hello world")
// Output: level=INFO msg="hello world" trace_id=74726163655f69645f74657374313233 span_id=7370616e5f696431
Use it as a middleware in slogmulti.Pipe:
import (
"github.com/go-slog/otelslog"
"github.com/samber/slog-multi"
)
handler = slogmulti.Pipe(otelslog.Middleware()).Handler(handler)
// Set p logger
// ...
Run tests:
go test -race -v ./...
Run linter:
golangci-lint run
The project is licensed under the MIT License.