From e092d8942780f81a2e4a255467b1a8bfcc111eef Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Wed, 8 Feb 2023 14:48:16 +0100 Subject: [PATCH] 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. --- ktesting/example_test.go | 23 +++++++++++++++++++++++ ktesting/options.go | 14 ++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/ktesting/example_test.go b/ktesting/example_test.go index 3fc688bf..9d887932 100644 --- a/ktesting/example_test.go +++ b/ktesting/example_test.go @@ -98,3 +98,26 @@ func ExampleNewLogger() { // E...] I failed err="failure" what="something" data={field:1} // I...] Logged at level 5. } + +func ExampleConfig_Verbosity() { + var buffer ktesting.BufferTL + config := ktesting.NewConfig(ktesting.Verbosity(1)) + logger := ktesting.NewLogger(&buffer, config) + + logger.Info("initial verbosity", "v", config.Verbosity().String()) + logger.V(2).Info("now you don't see me") + if err := config.Verbosity().Set("2"); err != nil { + logger.Error(err, "setting verbosity to 2") + } + logger.V(2).Info("now you see me") + if err := config.Verbosity().Set("1"); err != nil { + logger.Error(err, "setting verbosity to 1") + } + logger.V(2).Info("now I'm gone again") + + fmt.Print(headerRe.ReplaceAllString(buffer.String(), "${1}...] ")) + + // Output: + // I...] initial verbosity v="1" + // I...] now you see me +} diff --git a/ktesting/options.go b/ktesting/options.go index c9bb3da8..d039c40b 100644 --- a/ktesting/options.go +++ b/ktesting/options.go @@ -39,6 +39,20 @@ type Config struct { co configOptions } +// Verbosity returns a value instance that can be used to query (via String) or +// modify (via Set) the verbosity threshold. This is thread-safe and can be +// done at runtime. +func (c *Config) Verbosity() flag.Value { + return c.vstate.V() +} + +// VModule returns a value instance that can be used to query (via String) or +// modify (via Set) the vmodule settings. This is thread-safe and can be done +// at runtime. +func (c *Config) VModule() flag.Value { + return c.vstate.VModule() +} + // ConfigOption implements functional parameters for NewConfig. // // # Experimental