Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log/slog: slog sublog is lost when using a custom handler #65522

Closed
jayecc opened this issue Feb 5, 2024 · 1 comment
Closed

log/slog: slog sublog is lost when using a custom handler #65522

jayecc opened this issue Feb 5, 2024 · 1 comment

Comments

@jayecc
Copy link

jayecc commented Feb 5, 2024

Go version

go version go1.21

Output of go env in your module/workspace:

set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Administrator\AppData\Local\go-build
set GOENV=C:\Users\Administrator\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows

What did you do?

package main

import (
	"context"
	"fmt"
	"go.opentelemetry.io/otel"
	sdktrace "go.opentelemetry.io/otel/sdk/trace"
	"go.opentelemetry.io/otel/trace"
	"io"
	"log/slog"
	"os"
)

type Handler struct {
	slog.Handler
}

func NewHandler(w io.Writer, opts *slog.HandlerOptions) *Handler {
	return &Handler{Handler: slog.NewTextHandler(w, opts)}
}

func (h *Handler) Handle(ctx context.Context, r slog.Record) error {
	var attrs []slog.Attr
	spanCtx := trace.SpanContextFromContext(ctx)
	if spanCtx.HasTraceID() {
		attrs = append(attrs, slog.String("trace", spanCtx.TraceID().String()))
	}
	if spanCtx.HasSpanID() {
		attrs = append(attrs, slog.String("span", spanCtx.SpanID().String()))
	}
	if len(attrs) > 0 {
		r.AddAttrs(attrs...)
	}
	return h.Handler.Handle(ctx, r)
}

func init() {
	otel.SetTracerProvider(sdktrace.NewTracerProvider())
}

func main() {
	tracer := otel.Tracer("service")
	ctx, span := tracer.Start(context.Background(), "action")
	defer span.End()
	
	log := slog.New(NewHandler(os.Stdout, &slog.HandlerOptions{}))
	fmt.Printf("%#v\n", log)
	son := log.With(slog.String("son", "yes"))
	fmt.Printf("%#v\n", son)
	son.InfoContext(ctx, "hello")
	log.InfoContext(ctx, "hello")
}

What did you see happen?

&slog.Logger{handler:(*main.Handler)(0xc000026260)}
&slog.Logger{handler:(*slog.TextHandler)(0xc000050060)}
time=2024-02-05T17:32:58.936+08:00 level=INFO msg=hello son=yes
time=2024-02-05T17:32:58.959+08:00 level=INFO msg=hello trace=6b9fb5bc402858f63cee50c0aeab8a6a span=7c1828dafed7ea9c

What did you expect to see?

&slog.Logger{handler:(*main.Handler)(0xc000026260)}
&slog.Logger{handler:(*main.Handler)(0xc000050060)}
time=2024-02-05T17:32:58.936+08:00 level=INFO msg=hello son=yes trace=6b9fb5bc402858f63cee50c0aeab8a6a span=7c1828dafed7ea9c
time=2024-02-05T17:32:58.959+08:00 level=INFO msg=hello trace=6b9fb5bc402858f63cee50c0aeab8a6a span=7c1828dafed7ea9c
@jayecc jayecc changed the title slog sublog is lost when using a custom handler "log/slog" slog sublog is lost when using a custom handler Feb 5, 2024
@jayecc jayecc changed the title "log/slog" slog sublog is lost when using a custom handler log/slog: slog sublog is lost when using a custom handler Feb 5, 2024
@seankhliao
Copy link
Member

That's a problem of your handler implementation not handling WithGroup etc.

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants