Skip to content

Commit

Permalink
changed levels to log preffix and simplified unmarshal
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorhugoro1 committed Jun 17, 2024
1 parent bb3cdba commit d03c47a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 63 deletions.
8 changes: 4 additions & 4 deletions instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var errUndefinedTarget = fmt.Errorf("undefined target Go binary, consider settin
func newLogger(logLevel LogLevel) logr.Logger {
level, logErr := zap.ParseAtomicLevel(logLevel.String())
if logErr != nil {
level, _ = zap.ParseAtomicLevel(LevelInfo.String())
level, _ = zap.ParseAtomicLevel(LogLevelInfo.String())
}

config := zap.NewProductionConfig()
Expand Down Expand Up @@ -222,8 +222,8 @@ func newInstConfig(ctx context.Context, opts []InstrumentationOption) (instConfi
c.sampler = trace.AlwaysSample()
}

if c.logLevel == LevelUndefined {
c.logLevel = LevelInfo
if c.logLevel == logLevelUndefined {
c.logLevel = LogLevelInfo
}

return c, err
Expand Down Expand Up @@ -403,7 +403,7 @@ func WithEnv() InstrumentationOption {
}
if l, ok := lookupEnv(envLogLevelKey); ok {
var e error
level, e := ParseLevel(l)
level, e := ParseLogLevel(l)

if err == nil {
c.logLevel = level
Expand Down
8 changes: 4 additions & 4 deletions instrumentation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestWithEnv(t *testing.T) {

c, err := newInstConfig(context.Background(), []InstrumentationOption{WithEnv()})
require.NoError(t, err)
assert.Equal(t, LevelDebug, c.logLevel)
assert.Equal(t, LogLevelDebug, c.logLevel)

const wrong = "invalid"

Expand Down Expand Up @@ -193,13 +193,13 @@ func TestWithLogLevel(t *testing.T) {

require.NoError(t, err)

assert.Equal(t, LevelError, c.logLevel)
assert.Equal(t, LogLevelError, c.logLevel)

c, err = newInstConfig(context.Background(), []InstrumentationOption{WithLogLevel(LevelInfo)})
c, err = newInstConfig(context.Background(), []InstrumentationOption{WithLogLevel(LogLevelInfo)})

require.NoError(t, err)

assert.Equal(t, LevelInfo, c.logLevel)
assert.Equal(t, LogLevelInfo, c.logLevel)
})

t.Run("Will Validate Input", func(t *testing.T) {
Expand Down
61 changes: 23 additions & 38 deletions level.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,28 @@ import (
"fmt"
)

// Level defines the log level which instrumentation uses.
// LogLevel defines the log level which instrumentation uses.
type LogLevel string

const (
// LevelUndefined is an unset log level, it should not be used.
LevelUndefined LogLevel = ""
// LevelDebug sets the logging level to log all messages.
LevelDebug LogLevel = "debug"
// LevelInfo sets the logging level to log only informational, warning, and error messages.
LevelInfo LogLevel = "info"
// LevelWarn sets the logging level to log only warning and error messages.
LevelWarn LogLevel = "warn"
// LevelError sets the logging level to log only error messages.
LevelError LogLevel = "error"
logLevelUndefined LogLevel = ""
// LogLevelDebug sets the logging level to log all messages.
LogLevelDebug LogLevel = "debug"
// LogLevelInfo sets the logging level to log only informational, warning, and error messages.
LogLevelInfo LogLevel = "info"
// LogLevelWarn sets the logging level to log only warning and error messages.
LogLevelWarn LogLevel = "warn"
// LogLevelError sets the logging level to log only error messages.
LogLevelError LogLevel = "error"
)

// String returns the string encoding of the Level l.
var errInvalidLogLevel = errors.New("invalid LogLevel")

// String returns the string encoding of the LogLevel l.
func (l LogLevel) String() string {
switch l {
case LevelDebug, LevelInfo, LevelWarn, LevelError, LevelUndefined:
case LogLevelDebug, LogLevelInfo, LogLevelWarn, LogLevelError, logLevelUndefined:
return string(l)
default:
return fmt.Sprintf("Level(%s)", string(l))
Expand All @@ -48,44 +50,27 @@ func (l LogLevel) String() string {

// UnmarshalText applies the LogLevel type when inputted text is valid.
func (l *LogLevel) UnmarshalText(text []byte) error {
if ok := l.unmarshalText(bytes.ToLower(text)); ok {
return nil
}
*l = LogLevel(bytes.ToLower(text))

return l.validate()
}

func (l *LogLevel) validate() error {
if l == nil {
return errors.New("can't parse nil values")
return errors.New("nil LogLevel")
}

if !l.unmarshalText(bytes.ToLower([]byte(l.String()))) {
return errors.New("log level value is not accepted")
}

return nil
}

func (l *LogLevel) unmarshalText(text []byte) bool {
switch string(text) {
case "debug":
*l = LevelDebug
case "info":
*l = LevelInfo
case "warn":
*l = LevelWarn
case "error":
*l = LevelError
switch *l {
case LogLevelDebug, LogLevelInfo, LogLevelWarn, LogLevelError:
// Valid.
default:
return false
return fmt.Errorf("%w: %s", errInvalidLogLevel, l.String())
}

return true
return nil
}

// ParseLevel return a new LogLevel using text, and will return err if inputted text is not accepted.
func ParseLevel(text string) (LogLevel, error) {
// ParseLogLevel return a new LogLevel parsed from text. A non-nil error is returned if text is not a valid LogLevel.
func ParseLogLevel(text string) (LogLevel, error) {
var level LogLevel

err := level.UnmarshalText([]byte(text))
Expand Down
29 changes: 12 additions & 17 deletions level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,28 @@ func TestLevel(t *testing.T) {
str string
}{
{
name: "LevelUndefined",
level: LevelUndefined,
name: "LogLevelUndefined",
level: logLevelUndefined,
str: "",
},
{
name: "LevelDebug",
level: LevelDebug,
name: "LogLevelDebug",
level: LogLevelDebug,
str: "debug",
},
{
name: "LevelInfo",
level: LevelInfo,
name: "LogLevelInfo",
level: LogLevelInfo,
str: "info",
},
{
name: "LevelWarn",
level: LevelWarn,
name: "LogLevelWarn",
level: LogLevelWarn,
str: "warn",
},
{
name: "LevelError",
level: LevelError,
name: "LogLevelError",
level: LogLevelError,
str: "error",
},
}
Expand All @@ -61,11 +61,6 @@ func TestLevel(t *testing.T) {
}

func TestValidate(t *testing.T) {
t.Run("can validate wrong log levels", func(t *testing.T) {
var l LogLevel

l.unmarshalText([]byte("notexist"))

assert.Equal(t, "log level value is not accepted", l.validate().Error(), "log level is not nil")
})
l := LogLevel("notexist")
assert.ErrorIs(t, l.validate(), errInvalidLogLevel)
}

0 comments on commit d03c47a

Please sign in to comment.