Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logger, SugaredLogger: Add Level method #1148

Merged
merged 1 commit into from Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions logger.go
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.InvalidLevel].
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
27 changes: 27 additions & 0 deletions logger_test.go
Expand Up @@ -83,6 +83,33 @@ func TestLoggerAtomicLevel(t *testing.T) {
})
}

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

for _, lvl := range levels {
lvl := lvl
t.Run(lvl.String(), func(t *testing.T) {
t.Parallel()

core, _ := observer.New(lvl)
log := New(core)
assert.Equal(t, lvl, log.Level())
})
}

t.Run("Nop", func(t *testing.T) {
assert.Equal(t, zapcore.InvalidLevel, 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
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.InvalidLevel].
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
29 changes: 29 additions & 0 deletions sugar_test.go
Expand Up @@ -139,6 +139,35 @@ func TestSugarWith(t *testing.T) {
}
}

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

for _, lvl := range levels {
lvl := lvl
t.Run(lvl.String(), func(t *testing.T) {
t.Parallel()

core, _ := observer.New(lvl)
log := New(core).Sugar()
assert.Equal(t, lvl, log.Level())
})
}

t.Run("Nop", func(t *testing.T) {
t.Parallel()

assert.Equal(t, zapcore.InvalidLevel, 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