From 535c94c2fe547195db1807def7450b4e68ec6915 Mon Sep 17 00:00:00 2001 From: andig Date: Mon, 15 Apr 2024 23:27:27 +0200 Subject: [PATCH] Log viewer: fix old data returned when count set --- util/logstash/log.go | 7 ++++--- util/logstash/log_test.go | 2 ++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/util/logstash/log.go b/util/logstash/log.go index 062d64754f..f7300e3f1e 100644 --- a/util/logstash/log.go +++ b/util/logstash/log.go @@ -100,11 +100,12 @@ func (l *logger) All(areas []string, level jww.Threshold, count int) []string { r = r.Next() if e, ok := r.Value.(element); ok && e != "" && (all || e.match(areas, level)) { res = append(res, string(e)) - if count > 0 && len(res) >= count { - break - } } } + if count > 0 && len(res) >= count { + res = res[len(res)-count:] + } + return res } diff --git a/util/logstash/log_test.go b/util/logstash/log_test.go index 173092bee9..87c07de7a0 100644 --- a/util/logstash/log_test.go +++ b/util/logstash/log_test.go @@ -26,6 +26,8 @@ func TestLog(t *testing.T) { assert.Equal(t, []string{s1, s3}, log.All([]string{"test1"}, jww.LevelTrace, 0)) assert.Equal(t, []string{s1, s2, s3}, log.All(nil, jww.LevelTrace, 0)) + assert.Equal(t, []string{s3}, log.All(nil, jww.LevelTrace, 1)) + assert.Nil(t, log.All(nil, jww.LevelFatal, 0)) assert.Equal(t, idx, log.data, "data should not be changed after All() call")