Skip to content

Commit e092d89

Browse files
committed
ktesting: support verbosity changes at runtime
Being able to change the verbosity at runtime is useful. It is already supported by the underlying code, ktesting and its Config struct just didn't expose it.
1 parent 90cff0f commit e092d89

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

ktesting/example_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,26 @@ func ExampleNewLogger() {
9898
// E...] I failed err="failure" what="something" data={field:1}
9999
// I...] Logged at level 5.
100100
}
101+
102+
func ExampleConfig_Verbosity() {
103+
var buffer ktesting.BufferTL
104+
config := ktesting.NewConfig(ktesting.Verbosity(1))
105+
logger := ktesting.NewLogger(&buffer, config)
106+
107+
logger.Info("initial verbosity", "v", config.Verbosity().String())
108+
logger.V(2).Info("now you don't see me")
109+
if err := config.Verbosity().Set("2"); err != nil {
110+
logger.Error(err, "setting verbosity to 2")
111+
}
112+
logger.V(2).Info("now you see me")
113+
if err := config.Verbosity().Set("1"); err != nil {
114+
logger.Error(err, "setting verbosity to 1")
115+
}
116+
logger.V(2).Info("now I'm gone again")
117+
118+
fmt.Print(headerRe.ReplaceAllString(buffer.String(), "${1}...] "))
119+
120+
// Output:
121+
// I...] initial verbosity v="1"
122+
// I...] now you see me
123+
}

ktesting/options.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ type Config struct {
3939
co configOptions
4040
}
4141

42+
// Verbosity returns a value instance that can be used to query (via String) or
43+
// modify (via Set) the verbosity threshold. This is thread-safe and can be
44+
// done at runtime.
45+
func (c *Config) Verbosity() flag.Value {
46+
return c.vstate.V()
47+
}
48+
49+
// VModule returns a value instance that can be used to query (via String) or
50+
// modify (via Set) the vmodule settings. This is thread-safe and can be done
51+
// at runtime.
52+
func (c *Config) VModule() flag.Value {
53+
return c.vstate.VModule()
54+
}
55+
4256
// ConfigOption implements functional parameters for NewConfig.
4357
//
4458
// # Experimental

0 commit comments

Comments
 (0)