Skip to content

Commit

Permalink
parity with log 1.2.0 for variadic LoggerFlagSetup (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly authored Feb 12, 2023
1 parent dec9c81 commit 93ac7e0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
14 changes: 10 additions & 4 deletions dynloglevel/dynloglevel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
23 changes: 23 additions & 0 deletions dynloglevel/dynloglevel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions examples/server_kube/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"fortio.org/dflag"
"fortio.org/dflag/configmap"
"fortio.org/dflag/dynloglevel"
"fortio.org/dflag/endpoint"
"fortio.org/log"
)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down

0 comments on commit 93ac7e0

Please sign in to comment.