Skip to content

Commit a0fea0c

Browse files
committed
textlogger: verbosity changes through public API
By embedding *verbosity.VState in Config, users of the package already had access to V and VModule, but that had two drawbacks: - not easy to discover - unclean separate between internal and external API Providing explicit functions is better.
1 parent e092d89 commit a0fea0c

File tree

4 files changed

+76
-6
lines changed

4 files changed

+76
-6
lines changed

ktesting/testinglogger.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ var _ TL = &BufferTL{}
107107
// that output will be printed via the global klog logger with
108108
// `<test name> leaked goroutine` as prefix.
109109
//
110+
// Verbosity can be modified at any time through the Config.V and
111+
// Config.VModule API.
112+
//
110113
// # Experimental
111114
//
112115
// Notice: This type is EXPERIMENTAL and may be changed or removed in a

textlogger/example_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package textlogger_test
18+
19+
import (
20+
"bytes"
21+
"fmt"
22+
"regexp"
23+
24+
"k8s.io/klog/v2/textlogger"
25+
)
26+
27+
var headerRe = regexp.MustCompile(`([IE])[[:digit:]]{4} [[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}\.[[:digit:]]{6}[[:space:]]+[[:digit:]]+ example_test.go:[[:digit:]]+\] `)
28+
29+
func ExampleConfig_Verbosity() {
30+
var buffer bytes.Buffer
31+
config := textlogger.NewConfig(textlogger.Verbosity(1), textlogger.Output(&buffer))
32+
logger := textlogger.NewLogger(config)
33+
34+
logger.Info("initial verbosity", "v", config.Verbosity().String())
35+
logger.V(2).Info("now you don't see me")
36+
if err := config.Verbosity().Set("2"); err != nil {
37+
logger.Error(err, "setting verbosity to 2")
38+
}
39+
logger.V(2).Info("now you see me")
40+
if err := config.Verbosity().Set("1"); err != nil {
41+
logger.Error(err, "setting verbosity to 1")
42+
}
43+
logger.V(2).Info("now I'm gone again")
44+
45+
fmt.Print(headerRe.ReplaceAllString(buffer.String(), "${1}...] "))
46+
47+
// Output:
48+
// I...] "initial verbosity" v="1"
49+
// I...] "now you see me"
50+
}

textlogger/options.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,22 @@ import (
3636
// Notice: This type is EXPERIMENTAL and may be changed or removed in a
3737
// later release.
3838
type Config struct {
39-
*verbosity.VState
40-
co configOptions
39+
vstate *verbosity.VState
40+
co configOptions
41+
}
42+
43+
// Verbosity returns a value instance that can be used to query (via String) or
44+
// modify (via Set) the verbosity threshold. This is thread-safe and can be
45+
// done at runtime.
46+
func (c *Config) Verbosity() flag.Value {
47+
return c.vstate.V()
48+
}
49+
50+
// VModule returns a value instance that can be used to query (via String) or
51+
// modify (via Set) the vmodule settings. This is thread-safe and can be done
52+
// at runtime.
53+
func (c *Config) VModule() flag.Value {
54+
return c.vstate.VModule()
4155
}
4256

4357
// ConfigOption implements functional parameters for NewConfig.
@@ -111,7 +125,7 @@ func Output(output io.Writer) ConfigOption {
111125
// later release.
112126
func NewConfig(opts ...ConfigOption) *Config {
113127
c := &Config{
114-
VState: verbosity.New(),
128+
vstate: verbosity.New(),
115129
co: configOptions{
116130
verbosityFlagName: "v",
117131
vmoduleFlagName: "vmodule",
@@ -123,7 +137,7 @@ func NewConfig(opts ...ConfigOption) *Config {
123137
opt(&c.co)
124138
}
125139

126-
c.V().Set(strconv.FormatInt(int64(c.co.verbosityDefault), 10))
140+
c.Verbosity().Set(strconv.FormatInt(int64(c.co.verbosityDefault), 10))
127141
return c
128142
}
129143

@@ -134,6 +148,6 @@ func NewConfig(opts ...ConfigOption) *Config {
134148
// Notice: This function is EXPERIMENTAL and may be changed or removed in a
135149
// later release.
136150
func (c *Config) AddFlags(fs *flag.FlagSet) {
137-
fs.Var(c.V(), c.co.verbosityFlagName, "number for the log level verbosity of the testing logger")
151+
fs.Var(c.Verbosity(), c.co.verbosityFlagName, "number for the log level verbosity of the testing logger")
138152
fs.Var(c.VModule(), c.co.vmoduleFlagName, "comma-separated list of pattern=N log level settings for files matching the patterns")
139153
}

textlogger/textlogger.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ var (
5050

5151
// NewLogger constructs a new logger.
5252
//
53+
// Verbosity can be modified at any time through the Config.V and
54+
// Config.VModule API.
55+
//
5356
// # Experimental
5457
//
5558
// Notice: This function is EXPERIMENTAL and may be changed or removed in a
@@ -82,7 +85,7 @@ func (l *tlogger) WithCallDepth(depth int) logr.LogSink {
8285
func (l *tlogger) Enabled(level int) bool {
8386
// Skip this function and the Logger.Info call, then
8487
// also any additional stack frames from WithCallDepth.
85-
return l.config.Enabled(verbosity.Level(level), 2+l.callDepth)
88+
return l.config.vstate.Enabled(verbosity.Level(level), 2+l.callDepth)
8689
}
8790

8891
func (l *tlogger) Info(level int, msg string, kvList ...interface{}) {

0 commit comments

Comments
 (0)