diff --git a/zapcore/sampler.go b/zapcore/sampler.go index 25f10ca1d..31ed96e12 100644 --- a/zapcore/sampler.go +++ b/zapcore/sampler.go @@ -197,12 +197,14 @@ func (s *sampler) Check(ent Entry, ce *CheckedEntry) *CheckedEntry { return ce } - counter := s.counts.get(ent.Level, ent.Message) - n := counter.IncCheckReset(ent.Time, s.tick) - if n > s.first && (n-s.first)%s.thereafter != 0 { - s.hook(ent, LogDropped) - return ce + if ent.Level >= _minLevel && ent.Level <= _maxLevel { + counter := s.counts.get(ent.Level, ent.Message) + n := counter.IncCheckReset(ent.Time, s.tick) + if n > s.first && (n-s.first)%s.thereafter != 0 { + s.hook(ent, LogDropped) + return ce + } + s.hook(ent, LogSampled) } - s.hook(ent, LogSampled) return s.Core.Check(ent, ce) } diff --git a/zapcore/sampler_test.go b/zapcore/sampler_test.go index 71db0f9bd..30152d957 100644 --- a/zapcore/sampler_test.go +++ b/zapcore/sampler_test.go @@ -224,3 +224,9 @@ func TestSamplerRaces(t *testing.T) { close(start) wg.Wait() } + +func TestSamplerBounds(t *testing.T) { + // Prove that out-of-bounds levels don't crash. + sampler, _ := fakeSampler(DebugLevel-1, time.Minute, 2, 3) + writeSequence(sampler, 1, DebugLevel-1) +}