Skip to content

log/slog - Array of Errors as Contextual Parameter prints empty objects - JsonHandler #63958

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
SKumarSpace opened this issue Nov 5, 2023 · 3 comments

Comments

@SKumarSpace
Copy link

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

$ go version
go version go1.21.3 windows/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
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Shrey\AppData\Local\go-build
set GOENV=C:\Users\Shrey\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Shrey\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Shrey\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.21.3
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\Shrey\AppData\Local\Temp\go-build232294602=/tmp/go-build -gno-record-gcc-switches

What did you do?

I'm trying to log an array of errors. When I use slog and pass the errors array as a contextual attribute, it prints an empty object.
If I convert the errors to a string array, it works as expected.

It only prints empty objects when using slog.NewJSONHandler.
slog.NewTextHandler works fine

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

package main

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

func main() {
	slogger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
	slog.SetDefault(slogger)

	testErrors := []error{
		fmt.Errorf("test error 1"),
		fmt.Errorf("test error 2"),
		fmt.Errorf("test error 3"),
	}

	slog.Warn("This does not work", "errors", testErrors)
	//slog.Warn("This does", "errors", ToStringArray(testErrors)) // This works as intended as it's a string array
}

func ToStringArray(errs []error) []string {
	var errStrings []string
	for _, err := range errs {
		errStrings = append(errStrings, err.Error())
	}

	return errStrings
}

What did you expect to see?

{"time":"2009-11-10T23:00:00Z","level":"WARN","msg":"This does not work","errors":["test error 1","test error 2","test error 3"]}

What did you see instead?

{"time":"2009-11-10T23:00:00Z","level":"WARN","msg":"This does not work","errors":[{},{},{}]}
@callthingsoff
Copy link
Contributor

callthingsoff commented Nov 6, 2023

func TestMarshalError(t *testing.T) {
	testErrors := []error{
		fmt.Errorf("test error 1"),
		fmt.Errorf("test error 2"),
		fmt.Errorf("test error 3"),
	}
	x, e := json.Marshal(testErrors)
	fmt.Println(string(x), e)
}
[{},{},{}] <nil>

Maybe it’s related to the json package.

@aimuz
Copy link
Contributor

aimuz commented Nov 6, 2023

rel: #5161

@seankhliao
Copy link
Member

see above

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Nov 6, 2023
@golang golang locked and limited conversation to collaborators Nov 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants