Skip to content

log/slog: Logger.With loses handler #73057

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

Closed
andig opened this issue Mar 26, 2025 · 8 comments
Closed

log/slog: Logger.With loses handler #73057

andig opened this issue Mar 26, 2025 · 8 comments
Labels
BugReport Issues describing a possible bug in the Go implementation.

Comments

@andig
Copy link
Contributor

andig commented Mar 26, 2025

What version of Go are you using (go version)?

$ go version
go version go1.24.1 darwin/arm64

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
AR='ar'
CC='cc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='c++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/andig/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/andig/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/sv/rs_453y57xj86xsbz3kw1mbc0000gn/T/go-build460389641=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/Users/andig/htdocs/slog/go.mod'
GOMODCACHE='/Users/andig/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/andig/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.24.1/libexec'
GOSUMDB='sum.golang.org'
GOTELEMETRY='on'
GOTELEMETRYDIR='/Users/andig/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.24.1/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.24.1'
GOWORK=''
PKG_CONFIG='pkg-config'
GOROOT/bin/go version: go version go1.24.1 darwin/arm64
GOROOT/bin/go tool compile -V: compile version go1.24.1
uname -v: Darwin Kernel Version 24.3.0: Thu Jan  2 20:24:06 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T8103
ProductName:		macOS
ProductVersion:		15.3.2
BuildVersion:		24D81
lldb --version: lldb-1600.0.39.109
Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)

What did you do?

Consider this play: https://play.golang.com/p/wqZSGx6tIXi

What did you expect to see?

A logger created with custom handler like

l1 := slog.New(handler)

should maintain this handler when creating a derived logger with

l2 := l1.With(attr...)

What did you see instead?

Deriving an slog.Logger using logger.With(...) loses the loggers handler. Instead, a default slog.TextHandler is assigned.

This is surprising and not documented: https://pkg.go.dev/log/slog#Logger.With.

@seankhliao
Copy link
Member

With corresponds to WithAttrs in the Handler interface: https://pkg.go.dev/log/slog#Handler

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 Mar 26, 2025
@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label Mar 26, 2025
@andig
Copy link
Contributor Author

andig commented Mar 26, 2025

With corresponds to WithAttrs in the Handler interface: https://pkg.go.dev/log/slog#Handler

@seankhliao I absolutely appreciate curt answers, but the docs don't convey this. They suggest a logger with additional attributes, not a default logger with attributes. That this may be similar to the handler is absolutely not clear.

Also Handler.WithAttrs is still the same handler, at least that's what I'd expect. For Logger it's not, so not sure what the answer applies here.

Please reopen. Thank you!

@andig
Copy link
Contributor Author

andig commented Mar 26, 2025

I understand why this happens but it should be mentioned in the documentation of Logger.With.

@jba
Copy link
Contributor

jba commented Mar 26, 2025

What should the doc for Logger.With say? Whatever contract it has with its handler it not something that the user of slog cares about.

I think the warning about embedding one handler in another should be in https://github.com/golang/example/blob/master/slog-handler-guide/README.md, and also a reference to that doc should appear in the doc for slog.Handler, not just the package doc.

@jba
Copy link
Contributor

jba commented Mar 26, 2025

Also, testing/slogtest should catch this problem. Does it?

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/661015 mentions this issue: log/slog: Handler doc points to handler guide

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/660956 mentions this issue: slog-handler-guide: warn against embedding

gopherbot pushed a commit that referenced this issue Mar 26, 2025
There's a link in the package doc, but there should be one here too.

For #73057.

Change-Id: I8f8fe73f20bb6dd49cdf23b5f7634a92d4f7add9
Reviewed-on: https://go-review.googlesource.com/c/go/+/661015
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Sean Liao <sean@liao.dev>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
gopherbot pushed a commit to golang/example that referenced this issue Mar 26, 2025
For golang/go#73057.

Change-Id: Id9ee1c0af03f9b49f6063aa946b6fe5cfbc02483
Reviewed-on: https://go-review.googlesource.com/c/example/+/660956
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation.
Projects
None yet
Development

No branches or pull requests

5 participants