From 3770ed40a60a49d3990f139e89c5fb2725c4d594 Mon Sep 17 00:00:00 2001 From: Dave Young Date: Fri, 10 Jan 2025 14:16:48 -0600 Subject: [PATCH] feat(kmsgLogWatcher): add logger to parser --- .../logwatchers/kmsg/log_watcher_linux.go | 2 ++ .../logwatchers/kmsg/logger.go | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 pkg/systemlogmonitor/logwatchers/kmsg/logger.go diff --git a/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_linux.go b/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_linux.go index ecc40294c..c8ff6f2c4 100644 --- a/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_linux.go +++ b/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_linux.go @@ -68,6 +68,8 @@ func (k *kernelLogWatcher) Watch() (<-chan *logtypes.Log, error) { if err != nil { return nil, fmt.Errorf("failed to create kmsg parser: %v", err) } + // set kmsg parser logger to klog + parser.SetLogger(&kmsgParserLogger{}) k.kmsgParser = parser } diff --git a/pkg/systemlogmonitor/logwatchers/kmsg/logger.go b/pkg/systemlogmonitor/logwatchers/kmsg/logger.go new file mode 100644 index 000000000..5a8dbd2de --- /dev/null +++ b/pkg/systemlogmonitor/logwatchers/kmsg/logger.go @@ -0,0 +1,21 @@ +package kmsg + +import ( + "k8s.io/klog/v2" +) + +// kmsgParserLogger is compatible with the kmsgparser.Logger interface +// redirecting the log messages to klog +type kmsgParserLogger struct{} + +func (k *kmsgParserLogger) Infof(format string, args ...interface{}) { + klog.Infof(format, args...) +} + +func (k *kmsgParserLogger) Errorf(format string, args ...interface{}) { + klog.Errorf(format, args...) +} + +func (k *kmsgParserLogger) Warningf(format string, args ...interface{}) { + klog.Warningf(format, args...) +}