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

panic when processing reflected slice field #9

Open
mhilton opened this issue Aug 20, 2022 · 0 comments
Open

panic when processing reflected slice field #9

mhilton opened this issue Aug 20, 2022 · 0 comments

Comments

@mhilton
Copy link

mhilton commented Aug 20, 2022

If a zap.Reflect field is provided then a reflect: call of reflect.Value.Elem on slice Value panic occurs.

For example this:

package main

import (
	"os"

	zaplogfmt "github.com/jsternberg/zap-logfmt"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func main() {
	config := zap.NewProductionEncoderConfig()
	logger := zap.New(zapcore.NewCore(
		zaplogfmt.NewEncoder(config),
		os.Stdout,
		zapcore.DebugLevel,
	))
	logger.Info("Hello World", zap.Reflect("v", []string{"hello", "world"}))
}

produces:

panic: reflect: call of reflect.Value.Elem on slice Value

goroutine 1 [running]:
reflect.Value.Elem({0x102473c60?, 0x1400000e210?, 0x14000125898?})
	/usr/local/go/src/reflect/value.go:1259 +0x1a4
github.com/jsternberg/zap-logfmt.(*logfmtEncoder).AddReflected(0x2?, {0x10240dc7b, 0x1}, {0x102473c60?, 0x1400000e210?})
	/Users/mhilton/Library/go/pkg/mod/github.com/jsternberg/[email protected]/encoder.go:111 +0x1a0
go.uber.org/zap/zapcore.Field.AddTo({{0x10240dc7b, 0x1}, 0x17, 0x0, {0x0, 0x0}, {0x102473c60, 0x1400000e210}}, {0x1024ab358, 0x1400007ede0?})
	/Users/mhilton/Library/go/pkg/mod/go.uber.org/[email protected]/zapcore/field.go:170 +0x6d4
github.com/jsternberg/zap-logfmt.addFields(...)
	/Users/mhilton/Library/go/pkg/mod/github.com/jsternberg/[email protected]/encoder.go:537
github.com/jsternberg/zap-logfmt.(*logfmtEncoder).EncodeEntry(0x1400007ed80, {0x0, {0xc0b842abd4bf62d8, 0x11b9c4, 0x1025a8260}, {0x0, 0x0}, {0x10240f485, 0xb}, {0x0, ...}, ...}, ...)
	/Users/mhilton/Library/go/pkg/mod/github.com/jsternberg/[email protected]/encoder.go:307 +0x3b0
go.uber.org/zap/zapcore.(*ioCore).Write(0go: exit 1
x1400007edb0, {0x0, {0xc0b842abd4bf62d8, 0x11b9c4, 0x1025a8260}, {0x0, 0x0}, {0x10240f485, 0xb}, {0x0, ...}, ...}, ...)
	/Users/mhilton/Library/go/pkg/mod/go.uber.org/[email protected]/zapcore/core.go:86 +0x54
go.uber.org/zap/zapcore.(*CheckedEntry).Write(0x14000101380, {0x1400012c1c0, 0x1, 0x1})
	/Users/mhilton/Library/go/pkg/mod/go.uber.org/[email protected]/zapcore/entry.go:255 +0x150
go.uber.org/zap.(*Logger).Info(0x14000062160?, {0x10240f485?, 0x2?}, {0x1400012c1c0, 0x1, 0x1})
	/Users/mhilton/Library/go/pkg/mod/go.uber.org/[email protected]/logger.go:213 +0x54
main.main()
	/Users/mhilton/src/zap-logfmt-test/main.go:18 +0x308
mhilton added a commit to mhilton/jsternberg-zap-logfmt that referenced this issue Aug 22, 2022
Fixes: jsternberg#9

Update the Addreflected handler to not panic when adding a slice
or array. If possible a reflected slice or array are handled in the
same way as if AddArray was used.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant