Skip to content

Commit 20446e3

Browse files
committed
Logger, SugaredLogger: Add Level method
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
1 parent 6087ffe commit 20446e3

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

logger.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,13 @@ func (log *Logger) With(fields ...Field) *Logger {
183183
return l
184184
}
185185

186+
// Level reports the minimum enabled level for this logger.
187+
//
188+
// For NopLoggers, this is [zapcore.UnknownLevel].
189+
func (log *Logger) Level() zapcore.Level {
190+
return zapcore.LevelOf(log.core)
191+
}
192+
186193
// Check returns a CheckedEntry if logging a message at the specified level
187194
// is enabled. It's a completely optional optimization; in high-performance
188195
// applications, Check can help avoid allocating a slice to hold fields.

logger_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,30 @@ func TestLoggerAtomicLevel(t *testing.T) {
8383
})
8484
}
8585

86+
func TestLoggerLevel(t *testing.T) {
87+
levels := []zapcore.Level{
88+
DebugLevel,
89+
InfoLevel,
90+
WarnLevel,
91+
ErrorLevel,
92+
DPanicLevel,
93+
PanicLevel,
94+
FatalLevel,
95+
}
96+
97+
for _, lvl := range levels {
98+
t.Run(lvl.String(), func(t *testing.T) {
99+
core, _ := observer.New(lvl)
100+
log := New(core)
101+
assert.Equal(t, lvl, log.Level())
102+
})
103+
}
104+
105+
t.Run("Nop", func(t *testing.T) {
106+
assert.Equal(t, zapcore.UnknownLevel, NewNop().Level())
107+
})
108+
}
109+
86110
func TestLoggerInitialFields(t *testing.T) {
87111
fieldOpts := opts(Fields(Int("foo", 42), String("bar", "baz")))
88112
withLogger(t, DebugLevel, fieldOpts, func(logger *Logger, logs *observer.ObservedLogs) {

sugar.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ func (s *SugaredLogger) With(args ...interface{}) *SugaredLogger {
114114
return &SugaredLogger{base: s.base.With(s.sweetenFields(args)...)}
115115
}
116116

117+
// Level reports the minimum enabled level for this logger.
118+
//
119+
// For NopLoggers, this is [zapcore.UnknownLevel].
120+
func (s *SugaredLogger) Level() zapcore.Level {
121+
return zapcore.LevelOf(s.base.core)
122+
}
123+
117124
// Debug uses fmt.Sprint to construct and log a message.
118125
func (s *SugaredLogger) Debug(args ...interface{}) {
119126
s.log(DebugLevel, "", args, nil)

sugar_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,30 @@ func TestSugarWith(t *testing.T) {
139139
}
140140
}
141141

142+
func TestSugaredLoggerLevel(t *testing.T) {
143+
levels := []zapcore.Level{
144+
DebugLevel,
145+
InfoLevel,
146+
WarnLevel,
147+
ErrorLevel,
148+
DPanicLevel,
149+
PanicLevel,
150+
FatalLevel,
151+
}
152+
153+
for _, lvl := range levels {
154+
t.Run(lvl.String(), func(t *testing.T) {
155+
core, _ := observer.New(lvl)
156+
log := New(core).Sugar()
157+
assert.Equal(t, lvl, log.Level())
158+
})
159+
}
160+
161+
t.Run("Nop", func(t *testing.T) {
162+
assert.Equal(t, zapcore.UnknownLevel, NewNop().Sugar().Level())
163+
})
164+
}
165+
142166
func TestSugarFieldsInvalidPairs(t *testing.T) {
143167
withSugar(t, DebugLevel, nil, func(logger *SugaredLogger, logs *observer.ObservedLogs) {
144168
logger.With(42, "foo", []string{"bar"}, "baz").Info("")

0 commit comments

Comments
 (0)