From 297f8f821518b19666876f1b217044532d09bc9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Str=C3=B3zik?= Date: Fri, 4 Nov 2022 08:17:33 +0100 Subject: [PATCH] Improve common logging to allow the use of AtomicLevel (#15999) * add new New method * add missing comments --- common/logging/logger/logger.go | 25 ++++++++++++++++++++----- common/logging/logger/logger_test.go | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/common/logging/logger/logger.go b/common/logging/logger/logger.go index 294145f2edf3..133df196f3fa 100644 --- a/common/logging/logger/logger.go +++ b/common/logging/logger/logger.go @@ -17,12 +17,31 @@ type Logger struct { zapLogger *zap.SugaredLogger } +/* +This function creates logger structure based on given format, atomicLevel and additional cores +AtomicLevel structure allows to change level dynamically +*/ +func NewWithAtomicLevel(format Format, atomicLevel zap.AtomicLevel, additionalCores ...zapcore.Core) (*Logger, error) { + return new(format, atomicLevel, additionalCores...) +} + +/* +This function creates logger structure based on given format, level and additional cores +*/ func New(format Format, level Level, additionalCores ...zapcore.Core) (*Logger, error) { filterLevel, err := level.ToZapLevel() if err != nil { return nil, errors.Wrap(err, "while getting zap log level") } + levelEnabler := zap.LevelEnablerFunc(func(incomingLevel zapcore.Level) bool { + return incomingLevel >= filterLevel + }) + + return new(format, levelEnabler, additionalCores...) +} + +func new(format Format, levelEnabler zapcore.LevelEnabler, additionalCores ...zapcore.Core) (*Logger, error) { encoder, err := format.ToZapEncoder() if err != nil { return nil, errors.Wrapf(err, "while getting encoding configuration for %s format", format) @@ -31,9 +50,7 @@ func New(format Format, level Level, additionalCores ...zapcore.Core) (*Logger, defaultCore := zapcore.NewCore( encoder, zapcore.Lock(os.Stderr), - zap.LevelEnablerFunc(func(incomingLevel zapcore.Level) bool { - return incomingLevel >= filterLevel - }), + levelEnabler, ) cores := append(additionalCores, defaultCore) return &Logger{zap.New(zapcore.NewTee(cores...), zap.AddCaller()).Sugar()}, nil @@ -53,7 +70,6 @@ func (l *Logger) WithContext() *zap.SugaredLogger { } /* -* By default the Fatal Error log will be in json format, because it's production default. */ func LogFatalError(format string, args ...interface{}) error { @@ -66,7 +82,6 @@ func LogFatalError(format string, args ...interface{}) error { } /* -* This function initialize klog which is used in k8s/go-client */ func InitKlog(log *Logger, level Level) error { diff --git a/common/logging/logger/logger_test.go b/common/logging/logger/logger_test.go index 2e53e7f31190..ac79973375e0 100644 --- a/common/logging/logger/logger_test.go +++ b/common/logging/logger/logger_test.go @@ -14,6 +14,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/zap" + "go.uber.org/zap/zapcore" "go.uber.org/zap/zaptest/observer" ) @@ -43,6 +44,26 @@ func TestLogger(t *testing.T) { t.Log(observedLogs.All()) }) + t.Run("should log debug log after changing atomic level", func(t *testing.T) { + // given + atomic := zap.NewAtomicLevel() + atomic.SetLevel(zapcore.WarnLevel) + core, observedLogs := observer.New(atomic) + log, err := logger.NewWithAtomicLevel(logger.JSON, atomic, core) + require.NoError(t, err) + zapLogger := log.WithContext() + + // when + zapLogger.Info("log anything") + require.Equal(t, 0, observedLogs.Len()) + + atomic.SetLevel(zapcore.InfoLevel) + zapLogger.Info("log anything 2") + + // then + require.Equal(t, 1, observedLogs.Len()) + }) + t.Run("should log in the right json format", func(t *testing.T) { // GIVEN oldStdErr := os.Stderr