forked from golang/glog
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The wrapper function solves two problems: - Types which inherit a String implementation from some embedded type and then don't overwrite it log just the embedded type as string. Example: KubeletConfig. - Types which have a generated String implementation and no MarshalLog get always logged as string, also in JSON. Example: all of the Kubernetes API types. Ideally, types should get fixed to handle both cases. klog.Format is the fallback for cases where that is not possible or not desirable: it implements String as an indented JSON dump and MarshalLog as value dump through reflection.
- Loading branch information
Showing
5 changed files
with
191 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package klog | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/go-logr/logr" | ||
) | ||
|
||
// Format wraps a value of an arbitrary type and implement fmt.Stringer and | ||
// logr.Marshaler for them. Stringer returns pretty-printed JSON. MarshalLog | ||
// returns the original value with a type that has no special methods, in | ||
// particular no MarshalLog or MarshalJSON. | ||
// | ||
// Wrapping values like that is useful when the value has a broken | ||
// implementation of these special functions (for example, a type which | ||
// inherits String from TypeMeta, but then doesn't re-implement String) or the | ||
// implementation produces output that is less readable or unstructured (for | ||
// example, the generated String functions for Kubernetes API types). | ||
func Format(obj interface{}) interface{} { | ||
return formatAny{Object: obj} | ||
} | ||
|
||
type formatAny struct { | ||
Object interface{} | ||
} | ||
|
||
func (f formatAny) String() string { | ||
var buffer strings.Builder | ||
encoder := json.NewEncoder(&buffer) | ||
encoder.SetIndent("", " ") | ||
if err := encoder.Encode(&f.Object); err != nil { | ||
return fmt.Sprintf("error marshaling %T to JSON: %v", f, err) | ||
} | ||
return buffer.String() | ||
} | ||
|
||
func (f formatAny) MarshalLog() interface{} { | ||
// Returning a pointer to a pointer ensures that zapr doesn't find a | ||
// fmt.Stringer or logr.Marshaler when it checks the type of the | ||
// value. It then falls back to reflection, which dumps the value being | ||
// pointed to (JSON doesn't have pointers). | ||
ptr := &f.Object | ||
return &ptr | ||
} | ||
|
||
var _ fmt.Stringer = formatAny{} | ||
var _ logr.Marshaler = formatAny{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package klog_test | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"k8s.io/klog/v2" | ||
|
||
"github.com/go-logr/logr" | ||
) | ||
|
||
func TestFormat(t *testing.T) { | ||
obj := config{ | ||
TypeMeta: TypeMeta{ | ||
Kind: "config", | ||
}, | ||
RealField: 42, | ||
} | ||
|
||
assertEqual(t, "kind is config", obj.String(), "config.String()") | ||
assertEqual(t, `{ | ||
"Kind": "config", | ||
"RealField": 42 | ||
} | ||
`, klog.Format(obj).(fmt.Stringer).String(), "Format(config).String()") | ||
// fmt.Sprintf would call String if it was available. | ||
str := fmt.Sprintf("%s", klog.Format(obj).(logr.Marshaler).MarshalLog()) | ||
if strings.Index(str, "kind is config") >= 0 { | ||
t.Errorf("fmt.Sprintf called TypeMeta.String for klog.Format(obj).MarshalLog():\n%s", str) | ||
} | ||
|
||
structured, err := json.Marshal(klog.Format(obj).(logr.Marshaler).MarshalLog()) | ||
if err != nil { | ||
t.Errorf("JSON Marshal: %v", err) | ||
} else { | ||
assertEqual(t, `{"Kind":"config","RealField":42}`, string(structured), "json.Marshal(klog.Format(obj).MarshalLog())") | ||
} | ||
} | ||
|
||
func assertEqual(t *testing.T, expected, actual, what string) { | ||
if expected != actual { | ||
t.Errorf("%s:\nExpected\n%s\nActual\n%s\n", what, expected, actual) | ||
} | ||
} | ||
|
||
type TypeMeta struct { | ||
Kind string | ||
} | ||
|
||
func (t TypeMeta) String() string { | ||
return "kind is " + t.Kind | ||
} | ||
|
||
func (t TypeMeta) MarshalLog() interface{} { | ||
return t.Kind | ||
} | ||
|
||
type config struct { | ||
TypeMeta | ||
|
||
RealField int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters