diff --git a/go.sum b/go.sum index c66c3063d..6d61a23c2 100644 --- a/go.sum +++ b/go.sum @@ -645,6 +645,7 @@ golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTk golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20190409202823-959b441ac422 h1:QzoH/1pFpZguR8NrRHLcO6jKqfv2zpuSqZLgdm7ZmjI= golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= diff --git a/log/klogv2/klogv2.go b/log/klogv2/klogv2.go new file mode 100644 index 000000000..298abaa91 --- /dev/null +++ b/log/klogv2/klogv2.go @@ -0,0 +1,95 @@ +// Copyright © 2021 The virtual-kubelet 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 klogv2 implements a virtual-kubelet/log.Logger using klogv2 as a backend +// +// You can use this by creating a klogv2 logger and calling `FromKlogv2(fields)`. +// If you want this to be the default logger for virtual-kubelet, set `log.L` to the value returned by `FromKlogv2` +// +// We recommend reading the klog conventions to build a better understanding of levels and when they should apply +// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md +package klogv2 + +import ( + "github.com/virtual-kubelet/virtual-kubelet/log" + "k8s.io/klog/v2" +) + +// adapter implements the `log.Logger` interface for klogv2 +type adapter struct { + fields map[string]interface{} +} + +// FromKlogv2 creates a new `log.Logger` from the provided entry +func FromKlogv2(fields map[string]interface{}) log.Logger { + return &adapter{fields} +} + +func (l *adapter) Debug(args ...interface{}) { + klog.V(4).Info(args, l.fields) +} + +func (l *adapter) Debugf(format string, args ...interface{}) { + klog.V(4).Infof(format, args, l.fields) +} + +func (l *adapter) Info(args ...interface{}) { + klog.Info(args, l.fields) +} + +func (l *adapter) Infof(format string, args ...interface{}) { + klog.Infof(format, args, l.fields) +} + +func (l *adapter) Warn(args ...interface{}) { + klog.Warning(args, l.fields) +} + +func (l *adapter) Warnf(format string, args ...interface{}) { + klog.Warningf(format, args, l.fields) +} + +func (l *adapter) Error(args ...interface{}) { + klog.Error(args, l.fields) +} + +func (l *adapter) Errorf(format string, args ...interface{}) { + klog.Errorf(format, args, l.fields) +} + +func (l *adapter) Fatal(args ...interface{}) { + klog.Fatal(args, l.fields) +} + +func (l *adapter) Fatalf(format string, args ...interface{}) { + klog.Fatalf(format, args, l.fields) +} + +// WithField adds a field to the log entry. +func (l *adapter) WithField(key string, val interface{}) log.Logger { + fields := map[string]interface{}{ + key: val, + } + return FromKlogv2(fields) +} + +// WithFields adds multiple fields to a log entry. +func (l *adapter) WithFields(fields log.Fields) log.Logger { + return FromKlogv2(fields) +} + +// WithError adds an error to the log entry +func (l *adapter) WithError(err error) log.Logger { + return l.WithField("err", err) +} diff --git a/log/klogv2/klogv2_test.go b/log/klogv2/klogv2_test.go new file mode 100644 index 000000000..d150ae850 --- /dev/null +++ b/log/klogv2/klogv2_test.go @@ -0,0 +1,28 @@ +// Copyright © 2021 The virtual-kubelet 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 klogv2 + +import ( + "testing" + + "github.com/virtual-kubelet/virtual-kubelet/log" +) + +func TestImplementsLoggerInterface(t *testing.T) { + l := FromKlogv2(nil) + + if _, ok := l.(log.Logger); !ok { + t.Fatal("does not implement log.Logger interface") + } +} diff --git a/log/logrus/logrus.go b/log/logrus/logrus.go index 644f13ab7..e208905cb 100644 --- a/log/logrus/logrus.go +++ b/log/logrus/logrus.go @@ -22,27 +22,27 @@ import ( "github.com/virtual-kubelet/virtual-kubelet/log" ) -// Adapter implements the `log.Logger` interface for logrus -type Adapter struct { +// adapter implements the `log.Logger` interface for logrus +type adapter struct { *logrus.Entry } // FromLogrus creates a new `log.Logger` from the provided entry func FromLogrus(entry *logrus.Entry) log.Logger { - return &Adapter{entry} + return &adapter{entry} } // WithField adds a field to the log entry. -func (l *Adapter) WithField(key string, val interface{}) log.Logger { +func (l *adapter) WithField(key string, val interface{}) log.Logger { return FromLogrus(l.Entry.WithField(key, val)) } // WithFields adds multiple fields to a log entry. -func (l *Adapter) WithFields(f log.Fields) log.Logger { +func (l *adapter) WithFields(f log.Fields) log.Logger { return FromLogrus(l.Entry.WithFields(logrus.Fields(f))) } // WithError adds an error to the log entry -func (l *Adapter) WithError(err error) log.Logger { +func (l *adapter) WithError(err error) log.Logger { return FromLogrus(l.Entry.WithError(err)) }