internal/plog: add Enabled()
Signed-off-by: Andrew Keesler <akeesler@vmware.com>
This commit is contained in:
parent
74a8005f92
commit
93ebd0f949
@ -1,4 +1,4 @@
|
|||||||
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved.
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
package plog
|
package plog
|
||||||
@ -7,6 +7,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
|
"k8s.io/klog/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
//nolint: gochecknoglobals
|
//nolint: gochecknoglobals
|
||||||
@ -30,3 +31,34 @@ func RemoveKlogGlobalFlags() {
|
|||||||
panic("unsupported global klog flag set")
|
panic("unsupported global klog flag set")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func klogLevelForPlogLevel(plogLevel LogLevel) (klogLevel klog.Level) {
|
||||||
|
switch plogLevel {
|
||||||
|
case LevelWarning:
|
||||||
|
klogLevel = klogLevelWarning // unset means minimal logs (Error and Warning)
|
||||||
|
case LevelInfo:
|
||||||
|
klogLevel = klogLevelInfo
|
||||||
|
case LevelDebug:
|
||||||
|
klogLevel = klogLevelDebug
|
||||||
|
case LevelTrace:
|
||||||
|
klogLevel = klogLevelTrace
|
||||||
|
case LevelAll:
|
||||||
|
klogLevel = klogLevelAll + 100 // make all really mean all
|
||||||
|
default:
|
||||||
|
klogLevel = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func getKlogLevel() klog.Level {
|
||||||
|
// hack around klog not exposing a Get method
|
||||||
|
for i := klog.Level(0); i < 256; i++ {
|
||||||
|
if klog.V(i).Enabled() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return i - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved.
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
package plog
|
package plog
|
||||||
@ -39,26 +39,20 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func ValidateAndSetLogLevelGlobally(level LogLevel) error {
|
func ValidateAndSetLogLevelGlobally(level LogLevel) error {
|
||||||
var klogLogLevel int
|
klogLevel := klogLevelForPlogLevel(level)
|
||||||
|
if klogLevel < 0 {
|
||||||
switch level {
|
|
||||||
case LevelWarning:
|
|
||||||
klogLogLevel = klogLevelWarning // unset means minimal logs (Error and Warning)
|
|
||||||
case LevelInfo:
|
|
||||||
klogLogLevel = klogLevelInfo
|
|
||||||
case LevelDebug:
|
|
||||||
klogLogLevel = klogLevelDebug
|
|
||||||
case LevelTrace:
|
|
||||||
klogLogLevel = klogLevelTrace
|
|
||||||
case LevelAll:
|
|
||||||
klogLogLevel = klogLevelAll + 100 // make all really mean all
|
|
||||||
default:
|
|
||||||
return errInvalidLogLevel
|
return errInvalidLogLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := logs.GlogSetter(strconv.Itoa(klogLogLevel)); err != nil {
|
if _, err := logs.GlogSetter(strconv.Itoa(int(klogLevel))); err != nil {
|
||||||
panic(err) // programmer error
|
panic(err) // programmer error
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Enabled returns whether the provided plog level is enabled, i.e., whether print statements at the
|
||||||
|
// provided level will show up.
|
||||||
|
func Enabled(level LogLevel) bool {
|
||||||
|
return getKlogLevel() >= klogLevelForPlogLevel(level)
|
||||||
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved.
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
package plog
|
package plog
|
||||||
@ -13,42 +13,50 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestValidateAndSetLogLevelGlobally(t *testing.T) {
|
func TestValidateAndSetLogLevelGlobally(t *testing.T) {
|
||||||
originalLogLevel := getKlogLevel(t)
|
originalLogLevel := getKlogLevel()
|
||||||
|
require.GreaterOrEqual(t, int(originalLogLevel), int(klog.Level(0)), "cannot get klog level")
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
level LogLevel
|
level LogLevel
|
||||||
wantLevel klog.Level
|
wantLevel klog.Level
|
||||||
wantErr string
|
wantEnabled []LogLevel
|
||||||
|
wantErr string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "unset",
|
name: "unset",
|
||||||
wantLevel: 0,
|
wantLevel: 0,
|
||||||
|
wantEnabled: []LogLevel{LevelWarning},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "warning",
|
name: "warning",
|
||||||
level: LevelWarning,
|
level: LevelWarning,
|
||||||
wantLevel: 0,
|
wantLevel: 0,
|
||||||
|
wantEnabled: []LogLevel{LevelWarning},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "info",
|
name: "info",
|
||||||
level: LevelInfo,
|
level: LevelInfo,
|
||||||
wantLevel: 2,
|
wantLevel: 2,
|
||||||
|
wantEnabled: []LogLevel{LevelWarning, LevelInfo},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "debug",
|
name: "debug",
|
||||||
level: LevelDebug,
|
level: LevelDebug,
|
||||||
wantLevel: 4,
|
wantLevel: 4,
|
||||||
|
wantEnabled: []LogLevel{LevelWarning, LevelInfo, LevelDebug},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "trace",
|
name: "trace",
|
||||||
level: LevelTrace,
|
level: LevelTrace,
|
||||||
wantLevel: 6,
|
wantLevel: 6,
|
||||||
|
wantEnabled: []LogLevel{LevelWarning, LevelInfo, LevelDebug, LevelTrace},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "all",
|
name: "all",
|
||||||
level: LevelAll,
|
level: LevelAll,
|
||||||
wantLevel: 108,
|
wantLevel: 108,
|
||||||
|
wantEnabled: []LogLevel{LevelWarning, LevelInfo, LevelDebug, LevelTrace, LevelAll},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid level",
|
name: "invalid level",
|
||||||
@ -66,26 +74,31 @@ func TestValidateAndSetLogLevelGlobally(t *testing.T) {
|
|||||||
|
|
||||||
err := ValidateAndSetLogLevelGlobally(tt.level)
|
err := ValidateAndSetLogLevelGlobally(tt.level)
|
||||||
require.Equal(t, tt.wantErr, errString(err))
|
require.Equal(t, tt.wantErr, errString(err))
|
||||||
require.Equal(t, tt.wantLevel, getKlogLevel(t))
|
require.Equal(t, tt.wantLevel, getKlogLevel())
|
||||||
|
|
||||||
|
if tt.wantEnabled != nil {
|
||||||
|
allLevels := []LogLevel{LevelWarning, LevelInfo, LevelDebug, LevelTrace, LevelAll}
|
||||||
|
for _, level := range allLevels {
|
||||||
|
if contains(tt.wantEnabled, level) {
|
||||||
|
require.Truef(t, Enabled(level), "wanted %q to be enabled", level)
|
||||||
|
} else {
|
||||||
|
require.False(t, Enabled(level), "did not want %q to be enabled", level)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
require.Equal(t, originalLogLevel, getKlogLevel(t))
|
require.Equal(t, originalLogLevel, getKlogLevel())
|
||||||
}
|
}
|
||||||
|
|
||||||
func getKlogLevel(t *testing.T) klog.Level {
|
func contains(haystack []LogLevel, needle LogLevel) bool {
|
||||||
t.Helper()
|
for _, hay := range haystack {
|
||||||
|
if hay == needle {
|
||||||
// hack around klog not exposing a Get method
|
return true
|
||||||
for i := klog.Level(0); i < 256; i++ {
|
|
||||||
if klog.V(i).Enabled() {
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
return i - 1
|
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
t.Fatal("unknown log level")
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func errString(err error) string {
|
func errString(err error) string {
|
||||||
|
Loading…
Reference in New Issue
Block a user