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

support logr.Marshaler #325

Merged
merged 1 commit into from May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions internal/serialize/keyvalues.go
Expand Up @@ -20,6 +20,8 @@ import (
"bytes"
"fmt"
"strconv"

"github.com/go-logr/logr"
)

// WithValues implements LogSink.WithValues. The old key/value pairs are
Expand Down Expand Up @@ -131,6 +133,24 @@ func KVListFormat(b *bytes.Buffer, keysAndValues ...interface{}) {
writeStringValue(b, true, v)
case error:
writeStringValue(b, true, ErrorToString(v))
case logr.Marshaler:
value := MarshalerToValue(v)
// A marshaler that returns a string is useful for
// delayed formatting of complex values. We treat this
// case like a normal string. This is useful for
// multi-line support.
//
// We could do this by recursively formatting a value,
// but that comes with the risk of infinite recursion
// if a marshaler returns itself. Instead we call it
// only once and rely on it returning the intended
// value directly.
switch value := value.(type) {
case string:
writeStringValue(b, true, value)
default:
writeStringValue(b, false, fmt.Sprintf("%+v", v))
}
case []byte:
// In https://github.com/kubernetes/klog/pull/237 it was decided
// to format byte slices with "%+q". The advantages of that are:
Expand Down Expand Up @@ -163,6 +183,18 @@ func StringerToString(s fmt.Stringer) (ret string) {
return
}

// MarshalerToValue invokes a marshaler and catches
// panics.
func MarshalerToValue(m logr.Marshaler) (ret interface{}) {
defer func() {
if err := recover(); err != nil {
ret = fmt.Sprintf("<panic: %s>", err)
}
}()
ret = m.MarshalLog()
return
}

// ErrorToString converts an error to a string,
// handling panics if they occur.
func ErrorToString(err error) (ret string) {
Expand Down
17 changes: 16 additions & 1 deletion test/output.go
Expand Up @@ -321,7 +321,13 @@ I output.go:<LINE>] "test" firstKey=1 secondKey=3
"MarshalLog() that panics": {
text: "marshaler panic",
values: []interface{}{"obj", faultyMarshaler{}},
expectedOutput: `I output.go:<LINE>] "marshaler panic" obj={}
expectedOutput: `I output.go:<LINE>] "marshaler panic" obj="<panic: fake MarshalLog panic>"
`,
},
"MarshalLog() that returns itself": {
text: "marshaler recursion",
values: []interface{}{"obj", recursiveMarshaler{}},
expectedOutput: `I output.go:<LINE>] "marshaler recursion" obj={}
`,
},
}
Expand Down Expand Up @@ -765,6 +771,15 @@ func (f faultyMarshaler) MarshalLog() interface{} {

var _ logr.Marshaler = faultyMarshaler{}

type recursiveMarshaler struct{}

// MarshalLog returns itself, which could cause the logger to recurse infinitely.
func (r recursiveMarshaler) MarshalLog() interface{} {
return r
}

var _ logr.Marshaler = recursiveMarshaler{}

type faultyError struct{}

// Error always panics.
Expand Down
6 changes: 5 additions & 1 deletion test/zapr.go
Expand Up @@ -137,8 +137,12 @@ I output.go:<LINE>] "odd WithValues" keyWithoutValue="(MISSING)"
`: `{"caller":"test/output.go:<LINE>","msg":"error panic","errError":"PANIC=fake Error panic"}
`,

`I output.go:<LINE>] "marshaler panic" obj={}
`I output.go:<LINE>] "marshaler panic" obj="<panic: fake MarshalLog panic>"
`: `{"caller":"test/output.go:<LINE>","msg":"marshaler panic","v":0,"objError":"PANIC=fake MarshalLog panic"}
`,

`I output.go:<LINE>] "marshaler recursion" obj={}
`: `{"caller":"test/output.go:<LINE>","msg":"marshaler recursion","v":0,"obj":{}}
`,

// klog.Info
Expand Down