Skip to content

Commit

Permalink
ktesting: support verbosity changes at runtime
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pohly committed Feb 9, 2023
1 parent 90cff0f commit e092d89
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
23 changes: 23 additions & 0 deletions ktesting/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 14 additions & 0 deletions ktesting/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e092d89

Please sign in to comment.