diff --git a/dynloglevel/dynloglevel.go b/dynloglevel/dynloglevel.go index 24ff9ca..c6277ea 100644 --- a/dynloglevel/dynloglevel.go +++ b/dynloglevel/dynloglevel.go @@ -29,14 +29,15 @@ import ( var done = false -// LoggerFlagSetup sets up the `loglevel` flag as a dynamic flag. -func LoggerFlagSetup() { +// LoggerFlagSetup sets up the `loglevel` flag as a dynamic flag +// (or another name if desired/passed). +func LoggerFlagSetup(optionalFlagName ...string) { if done { return // avoid redefining flag/make it ok for multiple function to init this. } // virtual dynLevel flag that maps back to actual level defVal := log.GetLogLevel().String() - usage := fmt.Sprintf("`loglevel`, one of %v", log.LevelToStrA) + usage := fmt.Sprintf("log `level`, one of %v", log.LevelToStrA) flag := dflag.New(defVal, usage).WithInputMutator( func(inp string) string { // The validation map has full lowercase and capitalized first letter version @@ -49,7 +50,12 @@ func LoggerFlagSetup() { func(old, newStr string) { _ = log.SetLogLevelStr(newStr) // will succeed as we just validated it first }) - dflag.Flag("loglevel", flag) + if len(optionalFlagName) == 0 { + optionalFlagName = []string{"loglevel"} + } + for _, name := range optionalFlagName { + dflag.Flag(name, flag) + } done = true } diff --git a/dynloglevel/dynloglevel_test.go b/dynloglevel/dynloglevel_test.go index 3d125c1..1d4bcfd 100644 --- a/dynloglevel/dynloglevel_test.go +++ b/dynloglevel/dynloglevel_test.go @@ -40,6 +40,29 @@ func TestSetLevelFLag(t *testing.T) { LoggerFlagSetup() } +func TestMultipleFlagNames(t *testing.T) { + done = false // reset the test above + LoggerFlagSetup("l1", "l2") + _ = log.SetLogLevel(log.Info) + err := flag.CommandLine.Set("l2", " deBUG\n") + if err != nil { + t.Errorf("unexpected error for valid level %v", err) + } + if flag.Lookup("l1").Value.String() != "debug" { + t.Errorf("l1 not synced with l2/not set to debug: %q", flag.Lookup("l1").Value.String()) + } + prev := log.SetLogLevel(log.Info) + if prev != log.Debug { + t.Errorf("unexpected level after setting debug %v", prev) + } + err = flag.CommandLine.Set("l1", "bogus") + if err == nil { + t.Errorf("Didn't get an error setting bogus level") + } + // no harm in calling it twice + LoggerFlagSetup() +} + func TestChangeFlagsDefaultErrCase1(t *testing.T) { defer func() { if r := recover(); r == nil { diff --git a/examples/server_kube/http.go b/examples/server_kube/http.go index a615c90..acaec51 100644 --- a/examples/server_kube/http.go +++ b/examples/server_kube/http.go @@ -11,6 +11,7 @@ import ( "fortio.org/dflag" "fortio.org/dflag/configmap" + "fortio.org/dflag/dynloglevel" "fortio.org/dflag/endpoint" "fortio.org/log" ) @@ -49,8 +50,9 @@ var ( ) func main() { - dynBool3 = dflag.FlagBool("example_bool3", dynBool3) - dynStr2 = dflag.Flag("example_str2", dynStr2) + dflag.FlagBool("example_bool3", dynBool3) + dflag.Flag("example_str2", dynStr2) + dynloglevel.LoggerFlagSetup() flag.Parse() u, err := configmap.Setup(flag.CommandLine, *dirPathWatch) if err != nil { diff --git a/go.mod b/go.mod index d40307a..415715b 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.19 require ( fortio.org/assert v1.1.3 - fortio.org/log v1.1.0 + fortio.org/log v1.2.0 github.com/fsnotify/fsnotify v1.6.0 golang.org/x/exp v0.0.0-20230210204819-062eb4c674ab ) diff --git a/go.sum b/go.sum index 729f3ff..2991ea0 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ fortio.org/assert v1.1.3 h1:zXm8xiNiKvq2xG/YQ3sONAg3287XUuklKIDdjyD9pyg= fortio.org/assert v1.1.3/go.mod h1:039mG+/iYDPO8Ibx8TrNuJCm2T2SuhwRI3uL9nHTTls= -fortio.org/log v1.1.0 h1:tCSTZwyLYXS2+1LD2edUoFzEzIFa1Fi75mpHwGoMU4Y= -fortio.org/log v1.1.0/go.mod h1:u/8/2lyczXq52aT5Nw6reD+3cR6m/EbS2jBiIYhgiTU= +fortio.org/log v1.2.0 h1:neeowTa+D4Wpi/t+nCRSXkOEx3V3NNAFcneCtkNCW+0= +fortio.org/log v1.2.0/go.mod h1:u/8/2lyczXq52aT5Nw6reD+3cR6m/EbS2jBiIYhgiTU= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= golang.org/x/exp v0.0.0-20230210204819-062eb4c674ab h1:628ME69lBm9C6JY2wXhAph/yjN3jezx1z7BIDLUwxjo=