Skip to content

Commit 04d5230

Browse files
committed
add golog.IsEnabledFor()
1 parent bcec4af commit 04d5230

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

log/log.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ func SetLogfFunc(f func(msg string, args ...interface{}), level golog.Level) {
4545
// This function should be called before using below functions.
4646
func SetDefaultLogger(l *golog.Logger) {
4747
defaultLogger = l
48+
if l == nil {
49+
return
50+
}
4851
minLevel := l.GetMinLevel()
4952
for level := golog.DebugLevel; level <= golog.CritLevel; level++ {
5053
if level < minLevel {
@@ -87,6 +90,11 @@ func SetDefaultLogger(l *golog.Logger) {
8790
}
8891
}
8992

93+
// IsEnabledFor returns whether the default logger is enabled for the level.
94+
func IsEnabledFor(level golog.Level) bool {
95+
return defaultLogger != nil && defaultLogger.IsEnabledFor(level)
96+
}
97+
9098
func nop(args ...interface{}) {}
9199
func nopf(msg string, args ...interface{}) {}
92100

log/log_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,25 @@ func TestLogFuncs(t *testing.T) {
156156
l.Close()
157157
}
158158

159+
func TestIsEnabledFor(t *testing.T) {
160+
SetDefaultLogger(nil)
161+
if IsEnabledFor(golog.DebugLevel) {
162+
t.Error("nil logger is enabled for debug level")
163+
}
164+
165+
l := golog.NewStdoutLogger()
166+
SetDefaultLogger(l)
167+
if IsEnabledFor(golog.DebugLevel) {
168+
t.Error("info logger is enabled for debug level")
169+
}
170+
if !IsEnabledFor(golog.InfoLevel) {
171+
t.Error("info logger is disabled for info level")
172+
}
173+
if !IsEnabledFor(golog.ErrorLevel) {
174+
t.Error("info logger is disabled for error level")
175+
}
176+
}
177+
159178
func BenchmarkDiscardLogger(b *testing.B) {
160179
golog.StartFastTimer()
161180
defer golog.StopFastTimer()

logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (l *Logger) AddHandler(h *Handler) {
7171
}
7272
}
7373

74-
// IsEnabledFor returns whether it's enabled for the level
74+
// IsEnabledFor returns whether it's enabled for the level.
7575
func (l *Logger) IsEnabledFor(level Level) bool {
7676
return l.minLevel <= level
7777
}

0 commit comments

Comments
 (0)