Skip to content

Commit

Permalink
Logger, SugaredLogger: Add Level method
Browse files Browse the repository at this point in the history
Add a `Level() Level` method on Logger and SugaredLogger
that reports the current minimum enabled log level for the logger.

This relies on the zapcore.LevelOf function added in #1147.

Resolves #1144
  • Loading branch information
abhinav committed Aug 15, 2022
1 parent 6087ffe commit 20446e3
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
7 changes: 7 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ func (log *Logger) With(fields ...Field) *Logger {
return l
}

// Level reports the minimum enabled level for this logger.
//
// For NopLoggers, this is [zapcore.UnknownLevel].
func (log *Logger) Level() zapcore.Level {
return zapcore.LevelOf(log.core)
}

// Check returns a CheckedEntry if logging a message at the specified level
// is enabled. It's a completely optional optimization; in high-performance
// applications, Check can help avoid allocating a slice to hold fields.
Expand Down
24 changes: 24 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ func TestLoggerAtomicLevel(t *testing.T) {
})
}

func TestLoggerLevel(t *testing.T) {
levels := []zapcore.Level{
DebugLevel,
InfoLevel,
WarnLevel,
ErrorLevel,
DPanicLevel,
PanicLevel,
FatalLevel,
}

for _, lvl := range levels {
t.Run(lvl.String(), func(t *testing.T) {
core, _ := observer.New(lvl)
log := New(core)
assert.Equal(t, lvl, log.Level())
})
}

t.Run("Nop", func(t *testing.T) {
assert.Equal(t, zapcore.UnknownLevel, NewNop().Level())
})
}

func TestLoggerInitialFields(t *testing.T) {
fieldOpts := opts(Fields(Int("foo", 42), String("bar", "baz")))
withLogger(t, DebugLevel, fieldOpts, func(logger *Logger, logs *observer.ObservedLogs) {
Expand Down
7 changes: 7 additions & 0 deletions sugar.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ func (s *SugaredLogger) With(args ...interface{}) *SugaredLogger {
return &SugaredLogger{base: s.base.With(s.sweetenFields(args)...)}
}

// Level reports the minimum enabled level for this logger.
//
// For NopLoggers, this is [zapcore.UnknownLevel].
func (s *SugaredLogger) Level() zapcore.Level {
return zapcore.LevelOf(s.base.core)
}

// Debug uses fmt.Sprint to construct and log a message.
func (s *SugaredLogger) Debug(args ...interface{}) {
s.log(DebugLevel, "", args, nil)
Expand Down
24 changes: 24 additions & 0 deletions sugar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,30 @@ func TestSugarWith(t *testing.T) {
}
}

func TestSugaredLoggerLevel(t *testing.T) {
levels := []zapcore.Level{
DebugLevel,
InfoLevel,
WarnLevel,
ErrorLevel,
DPanicLevel,
PanicLevel,
FatalLevel,
}

for _, lvl := range levels {
t.Run(lvl.String(), func(t *testing.T) {
core, _ := observer.New(lvl)
log := New(core).Sugar()
assert.Equal(t, lvl, log.Level())
})
}

t.Run("Nop", func(t *testing.T) {
assert.Equal(t, zapcore.UnknownLevel, NewNop().Sugar().Level())
})
}

func TestSugarFieldsInvalidPairs(t *testing.T) {
withSugar(t, DebugLevel, nil, func(logger *SugaredLogger, logs *observer.ObservedLogs) {
logger.With(42, "foo", []string{"bar"}, "baz").Info("")
Expand Down

0 comments on commit 20446e3

Please sign in to comment.