Skip to content

Commit

Permalink
log fixes after move to zap logger
Browse files Browse the repository at this point in the history
- fix logging format
- initialize logger to log line number
- deal with log level update in daemon

Signed-off-by: adrianc <adrianc@nvidia.com>
  • Loading branch information
adrianchiris committed Oct 26, 2023
1 parent de8333a commit a829842
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion api/v1/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func IsSupportedModel(vendorID, deviceID string) bool {
return true
}
}
log.Info("IsSupportedModel():", "Unsupported model:", "vendorId:", vendorID, "deviceId:", deviceID)
log.Info("IsSupportedModel(): found unsupported model", "vendorId:", vendorID, "deviceId:", deviceID)
return false
}

Expand Down
9 changes: 8 additions & 1 deletion cmd/sriov-network-config-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"flag"

"github.com/spf13/cobra"
zzap "go.uber.org/zap"
"go.uber.org/zap/zapcore"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
Expand All @@ -37,7 +38,13 @@ var (

opts = &zap.Options{
Development: true,
TimeEncoder: zapcore.RFC3339NanoTimeEncoder,
// we dont log with panic level, so this essentially
// disables stacktrace, for now, it avoids un-needed clutter in logs
StacktraceLevel: zapcore.DPanicLevel,
TimeEncoder: zapcore.RFC3339NanoTimeEncoder,
// log caller (file and line number) in "caller" key
EncoderConfigOptions: []zap.EncoderConfigOption{func(ec *zapcore.EncoderConfig) { ec.CallerKey = "caller" }},
ZapOpts: []zzap.Option{zzap.AddCaller(), zzap.AddCallerSkip(1)},
}
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/sriov-network-config-daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func runStartCmd(cmd *cobra.Command, args []string) error {
setupLog.Error(err, "failed to fetch node state, exiting", "node-name", startOpts.nodeName)
return err
}
setupLog.V(0).Info("Running on platform: %s", platformType.String())
setupLog.Info("Running on", "platform", platformType.String())

var namespace = os.Getenv("NAMESPACE")
if err := sriovnetworkv1.InitNicIDMapFromConfigMap(kubeclient, namespace); err != nil {
Expand Down
15 changes: 11 additions & 4 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func New(
log.Log.Info(fmt.Sprintf("%s pod from Node %s/%s", verbStr, pod.Namespace, pod.Name))
},
Out: writer{log.Log.Info},
ErrOut: writer{func(msg string, kv ...interface{}) { log.Log.Error(nil, msg, kv) }},
ErrOut: writer{func(msg string, kv ...interface{}) { log.Log.Error(nil, msg, kv...) }},
Ctx: context.Background(),
},
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.NewMaxOfRateLimiter(
Expand Down Expand Up @@ -425,11 +425,18 @@ func (dn *Daemon) operatorConfigAddHandler(obj interface{}) {

func (dn *Daemon) operatorConfigChangeHandler(old, new interface{}) {
newCfg := new.(*sriovnetworkv1.SriovOperatorConfig)
newLevel := newCfg.Spec.LogLevel
if !log.Log.GetSink().Enabled(newLevel) {
newLevel := strconv.Itoa(newCfg.Spec.LogLevel)
currLevel := flag.Lookup("zap-log-level").Value.String()
if newLevel != currLevel {
log.Log.Info("Set log verbose level", "level", newLevel)
flag.Set("zap-log-level", strconv.Itoa(newLevel))
flag.Set("zap-log-level", newLevel)
} else {
log.Log.Info("current and new levels are the same")
}
log.Log.Info("I am default info level")
log.Log.V(0).Info("I am zero verbosity info level")
log.Log.V(1).Info("I am 1 verbosity info level")
log.Log.V(2).Info("I am 2 verbosity info level")
newDisableDrain := newCfg.Spec.DisableDrain
if dn.disableDrain != newDisableDrain {
dn.disableDrain = newDisableDrain
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ OUTER:
continue OUTER
}
}
log.Log.V(3).Info("CompareServices", "ServiceA", optsA, "ServiceB", *optB)
log.Log.V(2).Info("CompareServices", "ServiceA", optsA, "ServiceB", *optB)
return true, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ func IsKernelLockdownMode(chroot bool) bool {
path = "/host" + path
}
out, err := RunCommand("cat", path)
log.Log.V(2).Info("IsKernelLockdownMode()", "output", out, "error", err.Error())
log.Log.V(2).Info("IsKernelLockdownMode()", "output", out, "error", err)
if err != nil {
return false
}
Expand All @@ -917,7 +917,7 @@ func RunCommand(command string, args ...string) (string, error) {
cmd.Stderr = &stderr

err := cmd.Run()
log.Log.V(2).Info("RunCommand()", "output", stdout.String(), "error", err.Error())
log.Log.V(2).Info("RunCommand()", "output", stdout.String(), "error", err)
return stdout.String(), err
}

Expand Down

0 comments on commit a829842

Please sign in to comment.