diff --git a/instrumentation.go b/instrumentation.go index cee174229..3378d2c7a 100644 --- a/instrumentation.go +++ b/instrumentation.go @@ -95,7 +95,7 @@ func newLogger(logLevel LogLevel) logr.Logger { } if logErr != nil { - logger.V(2).Info("error to parse log level, changed to LevelInfo by default") + logger.Error(logErr, "invalid log level; using LevelInfo instead", zap.Error(logErr), zap.String("input", logLevel.String())) } return logger @@ -405,7 +405,7 @@ func WithEnv() InstrumentationOption { var e error level, e := ParseLogLevel(l) - if err == nil { + if e == nil { c.logLevel = level } @@ -520,7 +520,7 @@ func WithLoadedIndicator(indicator chan struct{}) InstrumentationOption { } // WithLogLevel returns an [InstrumentationOption] that will configure -// an [Instrumentation] with the logger level visibility defined as inputed. +// an [Instrumentation] to use the provided logging level. func WithLogLevel(level LogLevel) InstrumentationOption { return fnOpt(func(ctx context.Context, c instConfig) (instConfig, error) { if err := level.validate(); err != nil { diff --git a/level.go b/level.go index fbeb0e052..780b9d61b 100644 --- a/level.go +++ b/level.go @@ -24,7 +24,7 @@ import ( type LogLevel string const ( - // LevelUndefined is an unset log level, it should not be used. + // logLevelUndefined is an unset log level, it should not be used. logLevelUndefined LogLevel = "" // LogLevelDebug sets the logging level to log all messages. LogLevelDebug LogLevel = "debug" diff --git a/level_test.go b/level_test.go index b6e0e2db4..5fd8806e9 100644 --- a/level_test.go +++ b/level_test.go @@ -20,7 +20,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestLevel(t *testing.T) { +func TestLevelString(t *testing.T) { testCases := []struct { name string level LogLevel @@ -64,3 +64,46 @@ func TestValidate(t *testing.T) { l := LogLevel("notexist") assert.ErrorIs(t, l.validate(), errInvalidLogLevel) } + +func TestParseLogLevel(t *testing.T) { + testCases := []struct { + name string + str string + level LogLevel + }{ + { + name: "ParseLogLevelDebug", + str: "debug", + level: LogLevelDebug, + }, + { + name: "ParseLogLevelInfo", + str: "info", + level: LogLevelInfo, + }, + { + name: "ParseLogLevelWarn", + str: "warn", + level: LogLevelWarn, + }, + { + name: "ParseLogLevelError", + str: "error", + level: LogLevelError, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + l, _ := ParseLogLevel(tc.str) + + assert.Equal(t, tc.level, l, "LogLevel does not match") + }) + } + + t.Run("ParseNotExist", func(t *testing.T) { + _, err := ParseLogLevel("notexist") + + assert.ErrorIs(t, err, errInvalidLogLevel) + }) +}