Skip to content

log/slog: inconsistent handling of empty groups #61067

Closed
@seankhliao

Description

@seankhliao

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

$ go version
go version go1.21rc2 linux/amd64

Does this issue reproduce with the latest release?

slog is in the next release

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/user/.cache/go-build"
GOENV="/home/user/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS="-trimpath "-ldflags=-s -w""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/user/.data/go/pkg/mod"
GONOPROXY="github.com/snyk"
GONOSUMDB="github.com/snyk"
GOOS="linux"
GOPATH="/home/user/.data/go"
GOPRIVATE="github.com/snyk"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/golang"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/golang/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.5"
GCCGO="gccgo"
GOAMD64="v3"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="0"
GOMOD="/home/user/tmp/testrepo0123/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 -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3289887255=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
	"log/slog"
	"os"
)

func main() {
	h := slog.NewJSONHandler(os.Stdout, nil)
	l := slog.New(h)

	l.WithGroup("x").WithGroup("y").Info("foo")
	l.Info("foo", slog.Group("x", slog.Group("y")))
	l.Info("foo", slog.Group("x"))
}

What did you expect to see?

The documentation for slog.Handler.Handle states:

// Handle methods that produce output should observe the following rules:
// ...
// - If a group has no Attrs (even if it has a non-empty key),
// ignore it.

I expected for there to be no empty groups anywhere.

What did you see instead?

It doesn't seem to apply recursively, or apply to groups set with WithGroup:

{"time":"2023-06-29T16:13:32.030631211+01:00","level":"INFO","msg":"foo","x":{"y":{}}}
{"time":"2023-06-29T16:13:32.030684844+01:00","level":"INFO","msg":"foo","x":{}}
{"time":"2023-06-29T16:13:32.030687407+01:00","level":"INFO","msg":"foo"}

cc @jba

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions