From 181903f84616bcb8c1f42445a11efd417dcbfd64 Mon Sep 17 00:00:00 2001 From: Antonette Caldwell Date: Wed, 2 Aug 2023 13:23:44 -0500 Subject: [PATCH] set log level custom, and added formatting Signed-off-by: Antonette Caldwell --- logger/logger.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/logger/logger.go b/logger/logger.go index 3a941a0a..bce611be 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -1,6 +1,7 @@ package logger import ( + "fmt" "os" "time" @@ -16,15 +17,26 @@ type Handler interface { Warn(err error) Error(err error) + Infof(format string, args ...interface{}) + Debugf(format string, args ...interface{}) + Errorf(format string, args ...interface{}) + Warnf(format string, args ...interface{}) + // Kubernetes Controller compliant logger ControllerLogger() logr.Logger DatabaseLogger() gormlogger.Interface + + SetLevel(level logrus.Level) } type Logger struct { handler *logrus.Entry } +func (l *Logger) SetLevel(level logrus.Level) { + l.handler.Logger.SetLevel(level) +} + // TerminalFormatter is exported type TerminalFormatter struct{} @@ -80,18 +92,33 @@ func (l *Logger) Error(err error) { }).Log(logrus.ErrorLevel, err.Error()) } +func (l *Logger) Errorf(format string, args ...interface{}) { + message := fmt.Sprintf(format, args...) + l.handler.Logger.Error(message) +} + func (l *Logger) Info(description ...interface{}) { l.handler.Log(logrus.InfoLevel, description..., ) } +func (l *Logger) Infof(format string, args ...interface{}) { + message := fmt.Sprintf(format, args...) + l.handler.Log(logrus.InfoLevel, message) +} + func (l *Logger) Debug(description ...interface{}) { l.handler.Log(logrus.DebugLevel, description..., ) } +func (l *Logger) Debugf(format string, args ...interface{}) { + message := fmt.Sprintf(format, args...) + l.handler.Log(logrus.DebugLevel, message) +} + func (l *Logger) Warn(err error) { if err == nil { return @@ -105,3 +132,8 @@ func (l *Logger) Warn(err error) { "suggested-remediation": errors.GetRemedy(err), }).Log(logrus.WarnLevel, err.Error()) } + +func (l *Logger) Warnf(format string, args ...interface{}) { + message := fmt.Sprintf(format, args...) + l.handler.Log(logrus.WarnLevel, message) +}