Skip to content

Commit

Permalink
Use kv object instead of array for values encoding (#2)
Browse files Browse the repository at this point in the history
* Migrate to reproio

* Use kv object instead of array for values encoding

* remove tag
  • Loading branch information
mkanenobu authored Dec 16, 2024
1 parent e8a496c commit 23f878e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
8 changes: 2 additions & 6 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}))
Expand Down
38 changes: 38 additions & 0 deletions examples/logging_with_zap/main.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit 23f878e

Please sign in to comment.