diff --git a/internal/util/log.go b/internal/util/log.go index 7779a2b95..7ce064189 100644 --- a/internal/util/log.go +++ b/internal/util/log.go @@ -16,6 +16,17 @@ package util import ( "context" "fmt" + + klog "k8s.io/klog/v2" +) + +// enum defining logging levels. +const ( + Default klog.Level = iota + 1 + Useful + Extended + Debug + Trace ) type contextKey string @@ -40,3 +51,75 @@ func Log(ctx context.Context, format string) string { a += fmt.Sprintf("Req-ID: %v ", reqID) return a + format } + +// DefaultLog helps in logging with klog.level 1. +func DefaultLog(message string, args ...interface{}) { + logMessage := fmt.Sprintf(message, args...) + // If logging is disabled, don't evaluate the arguments + if klog.V(Default).Enabled() { + klog.InfoDepth(1, logMessage) + } +} + +// UsefulLog helps in logging with klog.level 2. +func UsefulLog(ctx context.Context, message string, args ...interface{}) { + logMessage := fmt.Sprintf(Log(ctx, message), args...) + // If logging is disabled, don't evaluate the arguments + if klog.V(Useful).Enabled() { + klog.InfoDepth(1, logMessage) + } +} + +// ExtendedLogMsg helps in logging a message with klog.level 3. +func ExtendedLogMsg(message string, args ...interface{}) { + logMessage := fmt.Sprintf(message, args...) + // If logging is disabled, don't evaluate the arguments + if klog.V(Extended).Enabled() { + klog.InfoDepth(1, logMessage) + } +} + +// ExtendedLog helps in logging with klog.level 3. +func ExtendedLog(ctx context.Context, message string, args ...interface{}) { + logMessage := fmt.Sprintf(Log(ctx, message), args...) + // If logging is disabled, don't evaluate the arguments + if klog.V(Extended).Enabled() { + klog.InfoDepth(1, logMessage) + } +} + +// DebugLogMsg helps in logging a message with klog.level 4. +func DebugLogMsg(message string, args ...interface{}) { + logMessage := fmt.Sprintf(message, args...) + // If logging is disabled, don't evaluate the arguments + if klog.V(Debug).Enabled() { + klog.InfoDepth(1, logMessage) + } +} + +// DebugLog helps in logging with klog.level 4. +func DebugLog(ctx context.Context, message string, args ...interface{}) { + logMessage := fmt.Sprintf(Log(ctx, message), args...) + // If logging is disabled, don't evaluate the arguments + if klog.V(Debug).Enabled() { + klog.InfoDepth(1, logMessage) + } +} + +// TraceLogMsg helps in logging a message with klog.level 5. +func TraceLogMsg(message string, args ...interface{}) { + logMessage := fmt.Sprintf(message, args...) + // If logging is disabled, don't evaluate the arguments + if klog.V(Trace).Enabled() { + klog.InfoDepth(1, logMessage) + } +} + +// TraceLog helps in logging with klog.level 5. +func TraceLog(ctx context.Context, message string, args ...interface{}) { + logMessage := fmt.Sprintf(Log(ctx, message), args...) + // If logging is disabled, don't evaluate the arguments + if klog.V(Trace).Enabled() { + klog.InfoDepth(1, logMessage) + } +} diff --git a/internal/util/util.go b/internal/util/util.go index 33551e538..c06c0f20c 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -35,15 +35,6 @@ import ( "k8s.io/utils/mount" ) -// enum defining logging levels. -const ( - Default klog.Level = iota + 1 - Useful - Extended - Debug - Trace -) - // RoundOffVolSize rounds up given quantity upto chunks of MiB/GiB. func RoundOffVolSize(size int64) int64 { size = RoundOffBytes(size) @@ -323,75 +314,3 @@ func contains(s []string, key string) bool { return false } - -// DefaultLog helps in logging with klog.level 1. -func DefaultLog(message string, args ...interface{}) { - logMessage := fmt.Sprintf(message, args...) - // If logging is disabled, don't evaluate the arguments - if klog.V(Default).Enabled() { - klog.InfoDepth(1, logMessage) - } -} - -// UsefulLog helps in logging with klog.level 2. -func UsefulLog(ctx context.Context, message string, args ...interface{}) { - logMessage := fmt.Sprintf(Log(ctx, message), args...) - // If logging is disabled, don't evaluate the arguments - if klog.V(Useful).Enabled() { - klog.InfoDepth(1, logMessage) - } -} - -// ExtendedLogMsg helps in logging a message with klog.level 3. -func ExtendedLogMsg(message string, args ...interface{}) { - logMessage := fmt.Sprintf(message, args...) - // If logging is disabled, don't evaluate the arguments - if klog.V(Extended).Enabled() { - klog.InfoDepth(1, logMessage) - } -} - -// ExtendedLog helps in logging with klog.level 3. -func ExtendedLog(ctx context.Context, message string, args ...interface{}) { - logMessage := fmt.Sprintf(Log(ctx, message), args...) - // If logging is disabled, don't evaluate the arguments - if klog.V(Extended).Enabled() { - klog.InfoDepth(1, logMessage) - } -} - -// DebugLogMsg helps in logging a message with klog.level 4. -func DebugLogMsg(message string, args ...interface{}) { - logMessage := fmt.Sprintf(message, args...) - // If logging is disabled, don't evaluate the arguments - if klog.V(Debug).Enabled() { - klog.InfoDepth(1, logMessage) - } -} - -// DebugLog helps in logging with klog.level 4. -func DebugLog(ctx context.Context, message string, args ...interface{}) { - logMessage := fmt.Sprintf(Log(ctx, message), args...) - // If logging is disabled, don't evaluate the arguments - if klog.V(Debug).Enabled() { - klog.InfoDepth(1, logMessage) - } -} - -// TraceLogMsg helps in logging a message with klog.level 5. -func TraceLogMsg(message string, args ...interface{}) { - logMessage := fmt.Sprintf(message, args...) - // If logging is disabled, don't evaluate the arguments - if klog.V(Trace).Enabled() { - klog.InfoDepth(1, logMessage) - } -} - -// TraceLog helps in logging with klog.level 5. -func TraceLog(ctx context.Context, message string, args ...interface{}) { - logMessage := fmt.Sprintf(Log(ctx, message), args...) - // If logging is disabled, don't evaluate the arguments - if klog.V(Trace).Enabled() { - klog.InfoDepth(1, logMessage) - } -}