Skip to content

Commit

Permalink
change some descriptions and added ParseLogLevel test
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorhugoro1 committed Jun 18, 2024
1 parent d03c47a commit 70e415f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
6 changes: 3 additions & 3 deletions instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -405,7 +405,7 @@ func WithEnv() InstrumentationOption {
var e error
level, e := ParseLogLevel(l)

if err == nil {
if e == nil {
c.logLevel = level
}

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion level.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
45 changes: 44 additions & 1 deletion level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
}

0 comments on commit 70e415f

Please sign in to comment.