Skip to content

Commit 7801166

Browse files
committed
improve test logging
1 parent b5c1ced commit 7801166

File tree

3 files changed

+42
-49
lines changed

3 files changed

+42
-49
lines changed

internal/log.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log"
77
"os"
8+
"strings"
89
)
910

1011
// TODO (ned): Revisit logging
@@ -27,3 +28,31 @@ func (l *logger) Printf(ctx context.Context, format string, v ...interface{}) {
2728
var Logger Logging = &logger{
2829
log: log.New(os.Stderr, "redis: ", log.LstdFlags|log.Lshortfile),
2930
}
31+
32+
func NewFilterLogger(substr []string) Logging {
33+
l := Logger
34+
if _, ok := l.(*logger); !ok {
35+
l = &logger{
36+
log: log.New(os.Stderr, "redis: ", log.LstdFlags|log.Lshortfile),
37+
}
38+
}
39+
return &filterLogger{logger: l, substr: substr}
40+
}
41+
42+
type filterLogger struct {
43+
logger Logging
44+
substr []string
45+
}
46+
47+
func (l *filterLogger) Printf(ctx context.Context, format string, v ...interface{}) {
48+
msg := fmt.Sprintf(format, v...)
49+
for _, substr := range l.substr {
50+
if strings.Contains(msg, substr) {
51+
return
52+
}
53+
}
54+
if l.logger != nil {
55+
l.logger.Printf(ctx, format, v...)
56+
return
57+
}
58+
}

internal/pool/pool_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010

1111
. "github.com/bsm/ginkgo/v2"
1212
. "github.com/bsm/gomega"
13+
"github.com/redis/go-redis/v9"
14+
"github.com/redis/go-redis/v9/internal"
1315

1416
"github.com/redis/go-redis/v9/internal/pool"
1517
)
@@ -436,3 +438,8 @@ var _ = Describe("race", func() {
436438
Expect(stats.Timeouts).To(Equal(uint32(1)))
437439
})
438440
})
441+
442+
func init() {
443+
filterLogger := internal.NewFilterLogger([]string{"test panic"})
444+
redis.SetLogger(filterLogger)
445+
}

main_test.go

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package redis_test
22

33
import (
4-
"context"
54
"fmt"
6-
"log"
75
"net"
86
"os"
97
"strconv"
@@ -15,6 +13,7 @@ import (
1513
. "github.com/bsm/ginkgo/v2"
1614
. "github.com/bsm/gomega"
1715
"github.com/redis/go-redis/v9"
16+
"github.com/redis/go-redis/v9/internal"
1817
)
1918

2019
const (
@@ -55,8 +54,6 @@ var (
5554
sentinel1, sentinel2, sentinel3 *redis.Client
5655
)
5756

58-
var TLogger *TestLogger
59-
6057
var cluster = &clusterScenario{
6158
ports: []string{"16600", "16601", "16602", "16603", "16604", "16605"},
6259
nodeIDs: make([]string, 6),
@@ -107,11 +104,11 @@ var _ = BeforeSuite(func() {
107104
fmt.Printf("REDIS_VERSION: %.1f\n", RedisVersion)
108105
fmt.Printf("CLIENT_LIBS_TEST_IMAGE: %v\n", os.Getenv("CLIENT_LIBS_TEST_IMAGE"))
109106

110-
// set logger that will filter some of the noise from the tests
111-
TLogger := NewTestLogger()
112-
TLogger.Filter("ERR unknown subcommand 'maint_notifications'")
113-
TLogger.Filter("test panic")
114-
redis.SetLogger(TLogger)
107+
filterLogger := internal.NewFilterLogger([]string{
108+
"ERR unknown subcommand 'maint_notifications'",
109+
"test panic",
110+
})
111+
redis.SetLogger(filterLogger)
115112

116113
if RedisVersion < 7.0 || RedisVersion > 9 {
117114
panic("incorrect or not supported redis version")
@@ -409,43 +406,3 @@ func (h *hook) ProcessPipelineHook(hook redis.ProcessPipelineHook) redis.Process
409406
}
410407
return hook
411408
}
412-
413-
func NewTestLogger() *TestLogger {
414-
intLogger := log.New(os.Stderr, "redis: ", log.LstdFlags|log.Lshortfile)
415-
return &TestLogger{
416-
intLogger,
417-
[]string{},
418-
}
419-
}
420-
421-
// TestLogger is a logger that filters out specific substrings so
422-
// the test output is not polluted with noise.
423-
type TestLogger struct {
424-
log *log.Logger
425-
filteredSubstrings []string
426-
}
427-
428-
// Filter adds a substring to the filter list.
429-
func (tl *TestLogger) Filter(substr string) {
430-
tl.filteredSubstrings = append(tl.filteredSubstrings, substr)
431-
}
432-
433-
// Unfilter removes a substring from the filter list.
434-
func (tl *TestLogger) Unfilter(substr string) {
435-
for i, s := range tl.filteredSubstrings {
436-
if s == substr {
437-
tl.filteredSubstrings = append(tl.filteredSubstrings[:i], tl.filteredSubstrings[i+1:]...)
438-
return
439-
}
440-
}
441-
}
442-
443-
func (tl *TestLogger) Printf(_ context.Context, format string, v ...interface{}) {
444-
msg := fmt.Sprintf(format, v...)
445-
for _, substr := range tl.filteredSubstrings {
446-
if strings.Contains(msg, substr) {
447-
return
448-
}
449-
}
450-
_ = tl.log.Output(2, msg)
451-
}

0 commit comments

Comments
 (0)