From c1784fbf2c61acf7e928290e514ae716c43b2580 Mon Sep 17 00:00:00 2001 From: Gaurav Ghildiyal Date: Fri, 10 Oct 2025 10:15:45 +0000 Subject: [PATCH] fix: Use klog for NRI library logging The NRI library uses its own logger interface, which defaults to logrus. To ensure consistent logging with the rest of the dranet components that use klog, this change introduces a klog-based logger for NRI. --- cmd/dranet/app.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/cmd/dranet/app.go b/cmd/dranet/app.go index d6dc5555..068460a6 100644 --- a/cmd/dranet/app.go +++ b/cmd/dranet/app.go @@ -28,6 +28,7 @@ import ( "sync/atomic" "syscall" + nrilog "github.com/containerd/nri/pkg/log" "github.com/google/cel-go/cel" "github.com/google/cel-go/ext" "github.com/google/dranet/pkg/driver" @@ -70,6 +71,7 @@ func init() { func main() { klog.InitFlags(nil) flag.Parse() + nrilog.Set(&nriLogger{logger: klog.Background().WithName("nri")}) printVersion() flag.VisitAll(func(f *flag.Flag) { @@ -184,3 +186,26 @@ func printVersion() { } klog.Infof("dranet go %s build: %s time: %s", info.GoVersion, vcsRevision, vcsTime) } + +// nriLogger is a klog-based implementation of the NRI logger interface, which +// allows the NRI library to log messages with the same structured format as the +// rest of the application. +type nriLogger struct { + logger klog.Logger +} + +func (l *nriLogger) Debugf(ctx context.Context, format string, args ...interface{}) { + l.logger.V(5).Info(fmt.Sprintf(format, args...)) +} + +func (l *nriLogger) Infof(ctx context.Context, format string, args ...interface{}) { + l.logger.Info(fmt.Sprintf(format, args...)) +} + +func (l *nriLogger) Warnf(ctx context.Context, format string, args ...interface{}) { + l.logger.Error(fmt.Errorf(format, args...), "NRI warning") +} + +func (l *nriLogger) Errorf(ctx context.Context, format string, args ...interface{}) { + l.logger.Error(fmt.Errorf(format, args...), "NRI error") +}