Skip to content

Commit

Permalink
Improve common logging to allow the use of AtomicLevel (#15999)
Browse files Browse the repository at this point in the history
* add new New method

* add missing comments
  • Loading branch information
pPrecel committed Nov 4, 2022
1 parent 05bebea commit 297f8f8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
25 changes: 20 additions & 5 deletions common/logging/logger/logger.go
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
21 changes: 21 additions & 0 deletions common/logging/logger/logger_test.go
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 297f8f8

Please sign in to comment.