Skip to content

Commit

Permalink
logger: add zap command line switches
Browse files Browse the repository at this point in the history
Allow to tune preconfigured by --log-mode zap backend with standard
zap command line switches from controller-runtime (zap-devel,
zap-encoder, zap-log-level, zap-stacktrace-level,
zap-time-encoding)[1].

This brings flexibility in logger setup for development environments
first of all.

The patch does not change default logger setup and does not change
DSCI override functionality.

[1] https://sdk.operatorframework.io/docs/building-operators/golang/references/logging

Signed-off-by: Yauheni Kaliuta <[email protected]>
  • Loading branch information
ykaliuta committed Oct 8, 2024
1 parent 0f382d5 commit 298b1d2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/healthz"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics/server"
ctrlwebhook "sigs.k8s.io/controller-runtime/pkg/webhook"
Expand Down Expand Up @@ -140,9 +141,12 @@ func main() { //nolint:funlen,maintidx
flag.StringVar(&operatorName, "operator-name", "opendatahub", "The name of the operator")
flag.StringVar(&logmode, "log-mode", "", "Log mode ('', prod, devel), default to ''")

opts := zap.Options{}
opts.BindFlags(flag.CommandLine)

flag.Parse()

ctrl.SetLogger(logger.NewLogger(logmode))
ctrl.SetLogger(logger.NewLoggerWithOptions(logmode, &opts))

// root context
ctx := ctrl.SetupSignalHandler()
Expand Down
46 changes: 45 additions & 1 deletion pkg/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logger

import (
"flag"
"os"
"strings"

Expand Down Expand Up @@ -35,9 +36,23 @@ func LogWithLevel(logger logr.Logger, level string) logr.Logger {
return logger.V(verbosityLevel)
}

func NewLoggerWithOptions(mode string, override *zap.Options) logr.Logger {
opts := newOptions(mode)
overrideOptions(opts, override)
return newLogger(opts)
}

// in DSC component, to use different mode for logging, e.g. development, production
// when not set mode it falls to "default" which is used by startup main.go.
func NewLogger(mode string) logr.Logger {
return newLogger(newOptions(mode))
}

func newLogger(opts *zap.Options) logr.Logger {
return zap.New(zap.UseFlagOptions(opts))
}

func newOptions(mode string) *zap.Options {
var opts zap.Options
switch mode {
case "devel", "development": // the most logging verbosity
Expand Down Expand Up @@ -72,5 +87,34 @@ func NewLogger(mode string) logr.Logger {
DestWriter: os.Stdout,
}
}
return zap.New(zap.UseFlagOptions(&opts))
return &opts
}

func overrideOptions(orig, override *zap.Options) {
// Development is boolean, cannot check for nil, so check if it was set
isDevelopmentSet := false
flag.Visit(func(f *flag.Flag) {
if f.Name == "zap-devel" {
isDevelopmentSet = true
}
})
if isDevelopmentSet {
orig.Development = override.Development
}

if override.StacktraceLevel != nil {
orig.StacktraceLevel = override.StacktraceLevel
}

if override.Level != nil {
orig.Level = override.Level
}

if override.DestWriter != nil {
orig.DestWriter = override.DestWriter
}

if override.EncoderConfigOptions != nil {
orig.EncoderConfigOptions = override.EncoderConfigOptions
}
}

0 comments on commit 298b1d2

Please sign in to comment.