k8s.io/component-base/logs: support changing verbosity of JSON output

The GlogSetter method is used by three components to change verbosity at
runtime through HTTP APIs. This used to work only for text output with klog
calls, but not for text output through the klog logger or for JSON output.

Now loggers can also provide a callback for changing their verbosity at
runtime. Implementing that implies that the Create factory method has to be
extended, which is an API break for the Go package, but not an API break for
the configuration file and command line flags, which is what matters for the
"api/v1" component API.
This commit is contained in:
Patrick Ohly
2022-12-20 16:05:46 +01:00
parent 8881b71822
commit 9b86f457e9
14 changed files with 249 additions and 23 deletions

View File

@@ -194,14 +194,18 @@ func benchmarkOutputFormats(b *testing.B, config loadGeneratorConfig, discard bo
}
b.Run("single-stream", func(b *testing.B) {
if discard {
logger, flush = logsjson.NewJSONLogger(c.Verbosity, logsjson.AddNopSync(&output), nil, nil)
l, control := logsjson.NewJSONLogger(c.Verbosity, logsjson.AddNopSync(&output), nil, nil)
logger = l
flush = control.Flush
} else {
stderr := os.Stderr
os.Stderr = out1
defer func() {
os.Stderr = stderr
}()
logger, flush = logsjson.Factory{}.Create(*c)
l, control := logsjson.Factory{}.Create(*c)
logger = l
flush = control.Flush
}
klog.SetLogger(logger)
defer klog.ClearLogger()
@@ -210,7 +214,9 @@ func benchmarkOutputFormats(b *testing.B, config loadGeneratorConfig, discard bo
b.Run("split-stream", func(b *testing.B) {
if discard {
logger, flush = logsjson.NewJSONLogger(c.Verbosity, logsjson.AddNopSync(&output), logsjson.AddNopSync(&output), nil)
l, control := logsjson.NewJSONLogger(c.Verbosity, logsjson.AddNopSync(&output), logsjson.AddNopSync(&output), nil)
logger = l
flush = control.Flush
} else {
stdout, stderr := os.Stdout, os.Stderr
os.Stdout, os.Stderr = out1, out2
@@ -219,7 +225,9 @@ func benchmarkOutputFormats(b *testing.B, config loadGeneratorConfig, discard bo
}()
c := logsapi.NewLoggingConfiguration()
c.Options.JSON.SplitStream = true
logger, flush = logsjson.Factory{}.Create(*c)
l, control := logsjson.Factory{}.Create(*c)
logger = l
flush = control.Flush
}
klog.SetLogger(logger)
defer klog.ClearLogger()