2021-01-26 22:34:27 +00:00
|
|
|
// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved.
|
2020-11-10 14:57:29 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package plog
|
|
|
|
|
|
|
|
import (
|
2022-04-16 02:43:53 +00:00
|
|
|
"go.uber.org/zap/zapcore"
|
2021-10-20 11:59:24 +00:00
|
|
|
"k8s.io/klog/v2"
|
2020-11-10 14:57:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// LogLevel is an enum that controls verbosity of logs.
|
|
|
|
// Valid values in order of increasing verbosity are leaving it unset, info, debug, trace and all.
|
|
|
|
type LogLevel string
|
|
|
|
|
2022-04-16 02:43:53 +00:00
|
|
|
func (l LogLevel) Enabled(_ zapcore.Level) bool {
|
|
|
|
return Enabled(l) // this basically says "log if the global plog level is l or greater"
|
|
|
|
}
|
|
|
|
|
2020-11-10 14:57:29 +00:00
|
|
|
const (
|
|
|
|
// LevelWarning (i.e. leaving the log level unset) maps to klog log level 0.
|
|
|
|
LevelWarning LogLevel = ""
|
|
|
|
// LevelInfo maps to klog log level 2.
|
|
|
|
LevelInfo LogLevel = "info"
|
|
|
|
// LevelDebug maps to klog log level 4.
|
|
|
|
LevelDebug LogLevel = "debug"
|
|
|
|
// LevelTrace maps to klog log level 6.
|
|
|
|
LevelTrace LogLevel = "trace"
|
|
|
|
// LevelAll maps to klog log level 100 (conceptually it is log level 8).
|
|
|
|
LevelAll LogLevel = "all"
|
|
|
|
)
|
|
|
|
|
2022-04-16 02:43:53 +00:00
|
|
|
var _ zapcore.LevelEnabler = LevelWarning
|
|
|
|
|
2020-11-10 14:57:29 +00:00
|
|
|
const (
|
|
|
|
klogLevelWarning = iota * 2
|
2022-04-16 02:43:53 +00:00
|
|
|
KlogLevelInfo
|
|
|
|
KlogLevelDebug
|
|
|
|
KlogLevelTrace
|
2020-11-10 14:57:29 +00:00
|
|
|
klogLevelAll
|
|
|
|
)
|
|
|
|
|
2021-01-26 22:34:27 +00:00
|
|
|
// 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 {
|
2022-04-16 02:43:53 +00:00
|
|
|
l := klogLevelForPlogLevel(level)
|
|
|
|
// check that both our global level and the klog global level agree that the plog level is enabled
|
|
|
|
// klog levels are inverted when zap handles them
|
|
|
|
return globalLevel.Enabled(zapcore.Level(-l)) && klog.V(l).Enabled()
|
|
|
|
}
|
|
|
|
|
|
|
|
func klogLevelForPlogLevel(plogLevel LogLevel) klog.Level {
|
|
|
|
switch plogLevel {
|
|
|
|
case LevelWarning:
|
|
|
|
return klogLevelWarning // unset means minimal logs (Error and Warning)
|
|
|
|
case LevelInfo:
|
|
|
|
return KlogLevelInfo
|
|
|
|
case LevelDebug:
|
|
|
|
return KlogLevelDebug
|
|
|
|
case LevelTrace:
|
|
|
|
return KlogLevelTrace
|
|
|
|
case LevelAll:
|
|
|
|
return klogLevelAll + 100 // make all really mean all
|
|
|
|
default:
|
|
|
|
return -1
|
|
|
|
}
|
2021-01-26 22:34:27 +00:00
|
|
|
}
|