From 0a252213a96e02a91ec944d612152ff1c5158ce6 Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Wed, 3 Jan 2024 00:41:12 +0530 Subject: [PATCH] add support to set loglevel Signed-off-by: MUzairS15 --- logger/logger.go | 15 +++++++++++---- logger/types.go | 10 ++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/logger/logger.go b/logger/logger.go index 3a941a0a..3a49903e 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -15,6 +15,8 @@ type Handler interface { Debug(description ...interface{}) Warn(err error) Error(err error) + SetLevel(level logrus.Level) + GetLevel() logrus.Level // Kubernetes Controller compliant logger ControllerLogger() logr.Logger @@ -57,10 +59,7 @@ func New(appname string, opts Options) (Handler, error) { log.SetOutput(opts.Output) } - log.SetLevel(logrus.InfoLevel) - if opts.DebugLevel { - log.SetLevel(logrus.DebugLevel) - } + log.SetLevel(logrus.Level(opts.LogLevel)) entry := log.WithFields(logrus.Fields{"app": appname}) return &Logger{handler: entry}, nil @@ -105,3 +104,11 @@ func (l *Logger) Warn(err error) { "suggested-remediation": errors.GetRemedy(err), }).Log(logrus.WarnLevel, err.Error()) } + +func (l *Logger) SetLevel(level logrus.Level) { + l.handler.Logger.SetLevel(level) +} + +func (l *Logger) GetLevel() logrus.Level { + return l.handler.Logger.GetLevel() +} diff --git a/logger/types.go b/logger/types.go index 1f9b6726..be49b575 100644 --- a/logger/types.go +++ b/logger/types.go @@ -1,6 +1,8 @@ package logger -import "io" +import ( + "io" +) const ( JsonLogFormat = iota @@ -11,7 +13,7 @@ const ( type Format int type Options struct { - Format Format - DebugLevel bool - Output io.Writer + Format Format + LogLevel int + Output io.Writer }