Skip to content

Commit

Permalink
added timestamp flag and subsequent behaviour changes for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mviswanathsai committed Jan 17, 2024
1 parent a9e3ca2 commit a42d7cf
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmd/cli/kubectl-kyverno/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Configure() error {
func configure(args ...string) error {
logging.InitFlags(nil)
if isVerbose(args...) {
return logging.Setup(logging.TextFormat, 0)
return logging.Setup(logging.TextFormat, logging.DefaultTime, 0)
}
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/internal/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (

var (
// logging
loggingFormat string
loggingFormat string
loggingTsFormat string
// profiling
profilingEnabled bool
profilingAddress string
Expand Down Expand Up @@ -61,6 +62,7 @@ var (
func initLoggingFlags() {
logging.InitFlags(nil)
flag.StringVar(&loggingFormat, "loggingFormat", logging.TextFormat, "This determines the output format of the logger.")
flag.StringVar(&loggingTsFormat, "loggingtsFormat", logging.DefaultTime, "This determines the timestamp format of the logger.")
checkErr(flag.Set("v", "2"), "failed to init flags")
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import (
func setupLogger() logr.Logger {
logLevel, err := strconv.Atoi(flag.Lookup("v").Value.String())
checkErr(err, "failed to setup logger")
checkErr(logging.Setup(loggingFormat, logLevel), "failed to setup logger")
checkErr(logging.Setup(loggingFormat, loggingTsFormat, logLevel), "failed to setup logger")
return logging.WithName("setup")
}
68 changes: 50 additions & 18 deletions pkg/logging/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"k8s.io/klog/v2"
"k8s.io/klog/v2/textlogger"
"sigs.k8s.io/controller-runtime/pkg/log"
)

Expand All @@ -28,13 +27,22 @@ const (
LogLevelController = 1
// LogLevelClient is the log level to use for clients.
LogLevelClient = 1
// time formats
DefaultTime = "default"
ISO8601 = "iso8601"
RFC3339 = "rfc3339"
MILLIS = "millis"
NANOS = "nanos"
EPOCH = "epoch"
RFC3339NANO = "rfc3339nano"
)

// Initially, globalLog comes from controller-runtime/log with logger created earlier by controller-runtime.
// When logging.Setup is called, globalLog is switched to the real logger.
// Call depth of all loggers created before logging.Setup will not work, including package level loggers as they are created before main.
// All loggers created after logging.Setup won't be subject to the call depth limitation and will work if the underlying sink supports it.
var globalLog = log.Log

var globalLog = log.Log //returns a Null log sink if SetLogger is not called.

Check failure on line 45 in pkg/logging/log.go

View workflow job for this annotation

GitHub Actions / tests

File is not `gofumpt`-ed (gofumpt)

func InitFlags(flags *flag.FlagSet) {
// clear flags initialized in static dependencies
Expand All @@ -46,28 +54,52 @@ func InitFlags(flags *flag.FlagSet) {

// Setup configures the logger with the supplied log format.
// It returns an error if the JSON logger could not be initialized or passed logFormat is not recognized.
func Setup(logFormat string, level int) error {
func Setup(logFormat string, loggingTimestampFormat string, level int) error {

Check failure on line 57 in pkg/logging/log.go

View workflow job for this annotation

GitHub Actions / tests

unnecessary leading newline (whitespace)

Check failure on line 58 in pkg/logging/log.go

View workflow job for this annotation

GitHub Actions / tests

File is not `gofumpt`-ed (gofumpt)
var zc zap.Config

switch logFormat {
case TextFormat:
config := textlogger.NewConfig(
textlogger.Verbosity(level),
textlogger.Output(os.Stdout),
)
globalLog = textlogger.NewLogger(config)
zc = zap.NewDevelopmentConfig()

case JSONFormat:
zc := zap.NewProductionConfig()
// Zap's levels get more and less verbose as the number gets smaller and higher respectively (DebugLevel is -1, InfoLevel is 0, WarnLevel is 1, and so on).
zc.Level = zap.NewAtomicLevelAt(zapcore.Level(-1 * level))
zapLog, err := zc.Build()
if err != nil {
return err
}
globalLog = zapr.NewLogger(zapLog)
// in json mode we configure klog and global logger to use zapr
klog.SetLogger(globalLog.WithName("klog"))
zc = zap.NewProductionConfig()

default:
return errors.New("log format not recognized, pass `text` for text mode or `json` to enable JSON logging")
}

//Configure the timestamp format

Check failure on line 72 in pkg/logging/log.go

View workflow job for this annotation

GitHub Actions / tests

File is not `gofumpt`-ed (gofumpt)
switch loggingTimestampFormat {
case ISO8601:
zc.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
case RFC3339:
zc.EncoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder
case MILLIS:
zc.EncoderConfig.EncodeTime = zapcore.EpochMillisTimeEncoder
case NANOS:
zc.EncoderConfig.EncodeTime = zapcore.EpochNanosTimeEncoder
case EPOCH:
zc.EncoderConfig.EncodeTime = zapcore.EpochTimeEncoder
case RFC3339NANO:
zc.EncoderConfig.EncodeTime = zapcore.RFC3339NanoTimeEncoder
case "default":
zc.EncoderConfig.EncodeTime = zapcore.EpochNanosTimeEncoder

default:
return errors.New("timestamp format not recognized, pass `iso8601` for ISO8601, `rfc3339` for RFC3339, `rfc3339nano` for RFC3339NANO, `millis` for Epoch Millis, `nanos` for Epoch Nanos, or omit the flag for the Unix Epoch timestamp format")
}

// Zap's levels get more and less verbose as the number gets smaller and higher respectively (DebugLevel is -1, InfoLevel is 0, WarnLevel is 1, and so on).
zc.Level = zap.NewAtomicLevelAt(zapcore.Level(-1 * level))
zapLog, err := zc.Build()
if err != nil {
return err
}
globalLog = zapr.NewLogger(zapLog)
// in json mode we configure klog and global logger to use zapr
klog.SetLogger(globalLog.WithName("klog"))

log.SetLogger(globalLog)
return nil
}
Expand Down

0 comments on commit a42d7cf

Please sign in to comment.