log: process fields only on first klog call
Signed-off-by: Pires <pjpires@gmail.com>
This commit is contained in:
@@ -24,6 +24,7 @@ import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/virtual-kubelet/virtual-kubelet/log"
|
||||
"k8s.io/klog/v2"
|
||||
@@ -32,17 +33,36 @@ import (
|
||||
// Ensure log.Logger is fully implemented during compile time.
|
||||
var _ log.Logger = (*adapter)(nil)
|
||||
|
||||
type fieldMap struct {
|
||||
log.Fields
|
||||
processedFields string
|
||||
sync.Once
|
||||
}
|
||||
|
||||
func (f *fieldMap) String() string {
|
||||
// Process if not processed before.
|
||||
f.Do(func() {
|
||||
// Only process if any fields have been set.
|
||||
if len(f.Fields) > 0 && len(f.processedFields) == 0 {
|
||||
f.processedFields = processFields(f.Fields)
|
||||
}
|
||||
})
|
||||
|
||||
return f.processedFields
|
||||
}
|
||||
|
||||
// adapter implements the `log.Logger` interface for klogv2
|
||||
type adapter struct {
|
||||
rawFields map[string]interface{}
|
||||
fields string
|
||||
fields fieldMap
|
||||
}
|
||||
|
||||
// New creates a new `log.Logger` from the provided entry
|
||||
func New(fields map[string]interface{}) log.Logger {
|
||||
func New(fields log.Fields) log.Logger {
|
||||
if fields == nil {
|
||||
fields = make(log.Fields)
|
||||
}
|
||||
return &adapter{
|
||||
rawFields: fields,
|
||||
fields: processFields(fields),
|
||||
fields: fieldMap{Fields: fields},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,43 +79,43 @@ func (l *adapter) Debugf(format string, args ...interface{}) {
|
||||
}
|
||||
|
||||
func (l *adapter) Info(args ...interface{}) {
|
||||
args = append(args, l.fields)
|
||||
args = append(args, l.fields.String())
|
||||
klog.InfoDepth(1, args...)
|
||||
}
|
||||
|
||||
func (l *adapter) Infof(format string, args ...interface{}) {
|
||||
formattedArgs := fmt.Sprintf(format, args...)
|
||||
klog.InfoDepth(1, formattedArgs, l.fields)
|
||||
klog.InfoDepth(1, formattedArgs, l.fields.String())
|
||||
}
|
||||
|
||||
func (l *adapter) Warn(args ...interface{}) {
|
||||
args = append(args, l.fields)
|
||||
args = append(args, l.fields.String())
|
||||
klog.WarningDepth(1, args...)
|
||||
}
|
||||
|
||||
func (l *adapter) Warnf(format string, args ...interface{}) {
|
||||
formattedArgs := fmt.Sprintf(format, args...)
|
||||
klog.WarningDepth(1, formattedArgs, l.fields)
|
||||
klog.WarningDepth(1, formattedArgs, l.fields.String())
|
||||
}
|
||||
|
||||
func (l *adapter) Error(args ...interface{}) {
|
||||
args = append(args, l.fields)
|
||||
args = append(args, l.fields.String())
|
||||
klog.ErrorDepth(1, args...)
|
||||
}
|
||||
|
||||
func (l *adapter) Errorf(format string, args ...interface{}) {
|
||||
formattedArgs := fmt.Sprintf(format, args...)
|
||||
klog.ErrorDepth(1, formattedArgs, l.fields)
|
||||
klog.ErrorDepth(1, formattedArgs, l.fields.String())
|
||||
}
|
||||
|
||||
func (l *adapter) Fatal(args ...interface{}) {
|
||||
args = append(args, l.fields)
|
||||
args = append(args, l.fields.String())
|
||||
klog.FatalDepth(1, args...)
|
||||
}
|
||||
|
||||
func (l *adapter) Fatalf(format string, args ...interface{}) {
|
||||
formattedArgs := fmt.Sprintf(format, args...)
|
||||
klog.FatalDepth(1, formattedArgs, l.fields)
|
||||
klog.FatalDepth(1, formattedArgs, l.fields.String())
|
||||
}
|
||||
|
||||
// WithField adds a field to the log entry.
|
||||
@@ -106,8 +126,8 @@ func (l *adapter) WithField(key string, val interface{}) log.Logger {
|
||||
// WithFields adds multiple fields to a log entry.
|
||||
func (l *adapter) WithFields(fields log.Fields) log.Logger {
|
||||
// Clone existing fields.
|
||||
newFields := make(map[string]interface{})
|
||||
for k, v := range l.rawFields {
|
||||
newFields := make(log.Fields)
|
||||
for k, v := range l.fields.Fields {
|
||||
newFields[k] = v
|
||||
}
|
||||
// Append new fields.
|
||||
|
||||
60
log/klogv2/klogv2_test.go
Normal file
60
log/klogv2/klogv2_test.go
Normal file
@@ -0,0 +1,60 @@
|
||||
// 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 TestFieldMap_String(t *testing.T) {
|
||||
var tests = []struct {
|
||||
fields fieldMap
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
fieldMap{Fields: nil},
|
||||
"",
|
||||
},
|
||||
{
|
||||
fieldMap{Fields: make(log.Fields)},
|
||||
"",
|
||||
},
|
||||
{
|
||||
fieldMap{Fields: map[string]interface{}{"one": 1}},
|
||||
" [one=1]",
|
||||
},
|
||||
{
|
||||
fieldMap{Fields: map[string]interface{}{"one": 1, "two": 2}},
|
||||
" [one=1 two=2]",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
// Assert fields haven't been processed yet.
|
||||
if len(tt.fields.processedFields) > 0 {
|
||||
t.Fatal("fields shouldn't have been processed yet")
|
||||
}
|
||||
// Assert fields have been processed, if any.
|
||||
actual := tt.fields.String()
|
||||
if len(tt.fields.Fields) > 0 && len(tt.fields.processedFields) == 0 {
|
||||
t.Fatal("fields should have been processed by now")
|
||||
}
|
||||
// Assert processFields yields desired results.
|
||||
if actual != tt.expected {
|
||||
t.Fatalf("expected: %s, got: %s", actual, tt.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user