Skip to content

log/slog: JSONHandler generates invalid JSON with empty Attr #62152

Closed
@jaloren

Description

@jaloren

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

1.21.0

Does this issue reproduce with the latest release?

yes

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

This also reproduces on the go playground.

go env Output
GO111MODULE=''
GOARCH='arm64'
GOBIN='/Users/jlorenzini/go/bin'
GOCACHE='/Users/jlorenzini/Library/Caches/go-build'
GOENV='/Users/jlorenzini/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/jlorenzini/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/jlorenzini/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.21.0'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/jlorenzini/repos/foo/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 -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/62/n3p69dvx45l3c3wgng9c01sw0000gn/T/go-build1832919248=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

I executed this code in a main function. This can also be reproduced in the go playground: https://go.dev/play/p/i7VtMtNK4eV

var buf bytes.Buffer
	f := func(groups []string, a slog.Attr) slog.Attr {
		return slog.Attr{}
	}
	h := slog.NewJSONHandler(&buf, &slog.HandlerOptions{

		ReplaceAttr: f})

	results := func() []map[string]any {
		var ms []map[string]any
		for _, line := range bytes.Split(buf.Bytes(), []byte{'\n'}) {
			if len(line) == 0 {
				continue
			}
			var m map[string]any
			if err := json.Unmarshal(line, &m); err != nil {
				panic(err) // In a real test, use t.Fatal.
			}
			ms = append(ms, m)
		}
		return ms
	}
	err := slogtest.TestHandler(h, results)
	if err != nil {
		log.Fatal(err)
	}

What did you expect to see?

The program run successfully without an error.

What did you see instead?

The program panicked with the following error when json unmarshal happened.

panic: invalid character ',' looking for beginning of object key string

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions