diff --git a/errors.go b/errors.go index 2d21c75..32d5b4a 100644 --- a/errors.go +++ b/errors.go @@ -327,13 +327,9 @@ func (x *Error) MarshalLogObject(enc zapcore.ObjectEncoder) error { if x.detail != "" { enc.AddString("detail", x.detail) } - enc.AddArray("values", zapcore.ArrayMarshalerFunc(func(inner zapcore.ArrayEncoder) error { + enc.AddObject("values", zapcore.ObjectMarshalerFunc(func(inner zapcore.ObjectEncoder) error { for k, v := range x.values { - inner.AppendObject(zapcore.ObjectMarshalerFunc(func(enc zapcore.ObjectEncoder) error { - enc.AddString("key", k) - enc.AddReflected("value", v) - return nil - })) + inner.AddReflected(k, v) } return nil })) diff --git a/examples/logging_with_zap/main.go b/examples/logging_with_zap/main.go new file mode 100644 index 0000000..4c97c04 --- /dev/null +++ b/examples/logging_with_zap/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "errors" + "github.com/reproio/goerr" + "go.uber.org/zap" + "log" +) + +type Nested struct { + NestedData string +} +type MyStruct struct { + Num int + Str string + Nested *Nested +} + +func main() { + logger, _ := zap.NewDevelopment() + defer logger.Sync() + + st := MyStruct{ + Num: 1, + Str: "str", + Nested: &Nested{ + NestedData: "nested", + }, + } + + err := goerr.Wrap(errors.New("some error")).WithValue("key", "value").WithValue("num_key", 1).WithValue("struct", st) + + if goErr := goerr.Unwrap(err); goErr != nil { + log.Printf("%s\n", goErr.LogValue()) + } + + logger.Error("something happening", zap.Object("error", err)) +}