mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2025-11-01 18:58:18 +00:00
Making the LoggingConfiguration part of the versioned component-base/config API
had the theoretic advantage that components could have offered different
configuration APIs with experimental features limited to alpha versions (for
example, sanitization offered only in a v1alpha1.KubeletConfiguration). Some
components could have decided to only use stable logging options.
In practice, this wasn't done. Furthermore, we don't want different components
to make different choices regarding which logging features they offer to
users. It should always be the same everywhere, for the sake of consistency.
This can be achieved with a saner Go API by dropping the distinction between
internal and external LoggingConfiguration types. Different stability levels of
indidividual fields have to be covered by documentation (done) and potentially
feature gates (not currently done).
Advantages:
- everything related to logging is under component-base/logs;
previously this was scattered across different packages and
different files under "logs" (why some code was in logs/config.go
vs. logs/options.go vs. logs/logs.go always confused me again
and again when coming back to the code):
- long-term config and command line API are clearly separated
into the "api" package underneath that
- logs/logs.go itself only deals with legacy global flags and
logging configuration
- removal of separate Go APIs like logs.BindLoggingFlags and
logs.Options
- LogRegistry becomes an implementation detail, with less code
and less exported functionality (only registration needs to
be exported, querying is internal)
79 lines
2.2 KiB
Go
79 lines
2.2 KiB
Go
/*
|
|
Copyright 2021 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 json
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/go-logr/logr"
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
logsapi "k8s.io/component-base/logs/api/v1"
|
|
logsjson "k8s.io/component-base/logs/json"
|
|
"k8s.io/klog/v2/test"
|
|
)
|
|
|
|
func init() {
|
|
test.InitKlog()
|
|
}
|
|
|
|
// TestJsonOutput tests the JSON logger, directly and as backend for klog.
|
|
func TestJSONOutput(t *testing.T) {
|
|
newLogger := func(out io.Writer, v int, vmodule string) logr.Logger {
|
|
logger, _ := logsjson.NewJSONLogger(logsapi.VerbosityLevel(v), logsjson.AddNopSync(out), nil,
|
|
&zapcore.EncoderConfig{
|
|
MessageKey: "msg",
|
|
CallerKey: "caller",
|
|
NameKey: "logger",
|
|
EncodeDuration: zapcore.StringDurationEncoder,
|
|
EncodeCaller: zapcore.ShortCallerEncoder,
|
|
})
|
|
return logger
|
|
}
|
|
|
|
// If Go modules are turned off (for example, as in "make test-integration"),
|
|
// references to klog like k8s.io/klog/v2.ObjectRef.MarshalLog become
|
|
// k8s.io/kubernetes/vendor/k8s.io/klog/v2.ObjectRef.MarshalLog.
|
|
injectVendor := func(mapping map[string]string) map[string]string {
|
|
if os.Getenv("GO111MODULE") != "off" {
|
|
return mapping
|
|
}
|
|
for key, value := range mapping {
|
|
mapping[key] = strings.ReplaceAll(value, "k8s.io/klog/v2", "k8s.io/kubernetes/vendor/k8s.io/klog/v2")
|
|
}
|
|
return mapping
|
|
}
|
|
|
|
t.Run("direct", func(t *testing.T) {
|
|
test.Output(t, test.OutputConfig{
|
|
NewLogger: newLogger,
|
|
ExpectedOutputMapping: injectVendor(test.ZaprOutputMappingDirect()),
|
|
})
|
|
})
|
|
|
|
t.Run("klog-backend", func(t *testing.T) {
|
|
test.Output(t, test.OutputConfig{
|
|
NewLogger: newLogger,
|
|
AsBackend: true,
|
|
ExpectedOutputMapping: injectVendor(test.ZaprOutputMappingIndirect()),
|
|
})
|
|
})
|
|
}
|