Skip to content

Commit

Permalink
fix: adjust stacktrace level on level change with SetLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
zbindenren committed Dec 24, 2020
1 parent c7d7592 commit f36e342
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 10 additions & 0 deletions flash.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,19 @@ func (l *Logger) Disable() {
// SetLevel sets the chosen level.
func (l *Logger) SetLevel(level zapcore.Level) {
l.m.Lock()
oldLevel := l.currentLevel
l.currentLevel = level
l.m.Unlock()
l.atom.SetLevel(level)

if level == zap.DebugLevel {
l.stackTrace(zap.ErrorLevel)
return
}

if oldLevel == zap.DebugLevel && level != zap.DebugLevel {
l.stackTrace(zap.FatalLevel)
}
}

// Get returns the embedded zap.Logger
Expand Down
13 changes: 11 additions & 2 deletions flash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,24 @@ func TestDisable(t *testing.T) {
assert.Empty(t, sink.String(), 0)
}

func TestSetLeve(t *testing.T) {
func TestSetLevelWithStacktrace(t *testing.T) {
defer sink.Reset()

l := flash.New(flash.WithSink("memory://"))
l := flash.New(flash.WithSink("memory://"), flash.WithStacktrace())
l.Debug("debug")
assert.Empty(t, sink.String(), 0)
l.Error("error")
assert.False(t, sink.containsStackTrace())
sink.Reset()
l.SetLevel(zapcore.DebugLevel)
l.Debug("debug")
assert.NotEmpty(t, sink.String(), 0)
l.Error("error")
assert.True(t, sink.containsStackTrace(), "no stacktrace for error level")
sink.Reset()
l.SetLevel(zapcore.InfoLevel)
l.Error("error")
assert.False(t, sink.containsStackTrace())
}

func TestWithPrometheus(t *testing.T) {
Expand Down

0 comments on commit f36e342

Please sign in to comment.