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: ReplaceAttr called with Group attribute, contradicting the docs #62731

Closed
pschultz opened this issue Sep 19, 2023 · 2 comments
Closed
Assignees
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@pschultz
Copy link

The Documention for slog.HandlerOptions.ReplaceAttr reads:

ReplaceAttr is never called for Group attributes, only their contents.

However, ReplaceAttr is indeed called with a Group attribute if -- and only if -- the Value is the result of resolving a LogValuer.

Either the call should not happen, or the documentation should mention this exception.

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

$ go version
go version go1.21.1 linux/amd64

Does this issue reproduce with the latest release?

Yes.

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

go env Output
$ go env
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/pschultz/.cache/go-build'
GOENV='/home/pschultz/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/pschultz/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/pschultz/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go1.21.1'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go1.21.1/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.1'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/tmp/tmp.QjZvF8MPOj/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build480343776=/tmp/go-build -gno-record-gcc-switches'

What did you do?

https://go.dev/play/p/uCI04fkKVIq

package main

import (
        "fmt"
        "io"
        "log/slog"
)

type Name struct {
        First, Last string
}

// LogValue implements slog.LogValuer.
// It returns a group containing the fields of
// the Name, so that they appear together in the log output.
func (n Name) LogValue() slog.Value {
        return slog.GroupValue(
                slog.String("first", n.First),
                slog.String("last", n.Last))
}

func main() {

        l := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{
                ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
                        fmt.Println(a.Key, a.Value.Kind())
                        return a
                },
        }))

        n := Name{"Perry", "Platypus"}
        l.Info("mission accomplished", "agent", n)
}

What did you expect to see?

time Time
level Any
msg String
first String
last String

What did you see instead?

time Time
level Any
msg String
agent Group // <-- this call should not happen
first String
last String
@jba jba self-assigned this Sep 21, 2023
@jba jba added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 21, 2023
@gopherbot
Copy link

Change https://go.dev/cl/530478 mentions this issue: log/slog: ensure ReplaceAttr does not see a group

@quki3
Copy link

quki3 commented Sep 25, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants