From f35102364db86214b5b0fc6610fb3add6850afef Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Mon, 13 Sep 2021 09:54:27 +0200 Subject: [PATCH] custom marshaler for ObjectRef This will be used by zapr for the JSON output format in Kubernetes. Without it, the String method will be used, which is not as intended. --- go.mod | 2 +- go.sum | 6 ++---- klog.go | 9 +++++++++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 08a2d0f3..31aefba7 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module k8s.io/klog/v2 go 1.13 -require github.com/go-logr/logr v1.0.0 +require github.com/go-logr/logr v1.2.0 diff --git a/go.sum b/go.sum index a1b90e4b..919fbadb 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,2 @@ -github.com/go-logr/logr v1.0.0-rc1 h1:+ul9F74rBkPajeP8m4o3o0tiglmzNFsPnuhYyBCQ0Sc= -github.com/go-logr/logr v1.0.0-rc1/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= -github.com/go-logr/logr v1.0.0 h1:kH951GinvFVaQgy/ki/B3YYmQtRpExGigSJg6O8z5jo= -github.com/go-logr/logr v1.0.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.2.0 h1:QK40JKJyMdUDz+h+xvCsru/bJhvG0UxvePV0ufL/AcE= +github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= diff --git a/klog.go b/klog.go index 60c88d3c..45efbb07 100644 --- a/klog.go +++ b/klog.go @@ -1591,6 +1591,15 @@ func (ref ObjectRef) String() string { return ref.Name } +// MarshalLog ensures that loggers with support for structured output will log +// as a struct by removing the String method via a custom type. +func (ref ObjectRef) MarshalLog() interface{} { + type or ObjectRef + return or(ref) +} + +var _ logr.Marshaler = ObjectRef{} + // KMetadata is a subset of the kubernetes k8s.io/apimachinery/pkg/apis/meta/v1.Object interface // this interface may expand in the future, but will always be a subset of the // kubernetes k8s.io/apimachinery/pkg/apis/meta/v1.Object interface