Skip to content

Commit 5b62309

Browse files
authored
feat: implement skip path regexps feature in zap package (#72)
- Add a new import statement for `regexp` in `zap.go` - Add a new field `SkipPathRegexps` to the `Config` struct in `zap.go` - Add a new function `GinzapWithConfig` in `zap.go` - Add a new test function `TestSkipPathRegexps` in `zap_test.go` Signed-off-by: appleboy <[email protected]>
1 parent 0abce4a commit 5b62309

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

zap.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"net/http/httputil"
99
"os"
10+
"regexp"
1011
"runtime/debug"
1112
"strings"
1213
"time"
@@ -29,11 +30,12 @@ type ZapLogger interface {
2930

3031
// Config is config setting for Ginzap
3132
type Config struct {
32-
TimeFormat string
33-
UTC bool
34-
SkipPaths []string
35-
Context Fn
36-
DefaultLevel zapcore.Level
33+
TimeFormat string
34+
UTC bool
35+
SkipPaths []string
36+
SkipPathRegexps []*regexp.Regexp
37+
Context Fn
38+
DefaultLevel zapcore.Level
3739
// skip is a Skipper that indicates which logs should not be written.
3840
// Optional.
3941
Skipper Skipper
@@ -70,6 +72,17 @@ func GinzapWithConfig(logger ZapLogger, conf *Config) gin.HandlerFunc {
7072
track = false
7173
}
7274

75+
if track && len(conf.SkipPathRegexps) > 0 {
76+
for _, reg := range conf.SkipPathRegexps {
77+
if !reg.MatchString(path) {
78+
continue
79+
}
80+
81+
track = false
82+
break
83+
}
84+
}
85+
7386
if track {
7487
end := time.Now()
7588
latency := end.Sub(start)

zap_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/http"
77
"net/http/httptest"
8+
"regexp"
89
"testing"
910
"time"
1011

@@ -178,3 +179,48 @@ func TestLoggerSkipper(t *testing.T) {
178179
t.Fatalf("logged path should be /test but %s", pathStr)
179180
}
180181
}
182+
183+
func TestSkipPathRegexps(t *testing.T) {
184+
ctx, cancel := context.WithCancel(context.Background())
185+
defer cancel()
186+
r := gin.New()
187+
188+
rxURL := regexp.MustCompile(`^/no_\s*`)
189+
190+
utcLogger, utcLoggerObserved := buildDummyLogger()
191+
r.Use(GinzapWithConfig(utcLogger, &Config{
192+
TimeFormat: time.RFC3339,
193+
UTC: true,
194+
SkipPathRegexps: []*regexp.Regexp{rxURL},
195+
}))
196+
197+
r.GET(testPath, func(c *gin.Context) {
198+
c.JSON(204, nil)
199+
})
200+
201+
r.GET("/no_log", func(c *gin.Context) {
202+
c.JSON(204, nil)
203+
})
204+
205+
res1 := httptest.NewRecorder()
206+
req1, _ := http.NewRequestWithContext(ctx, "GET", testPath, nil)
207+
r.ServeHTTP(res1, req1)
208+
209+
res2 := httptest.NewRecorder()
210+
req2, _ := http.NewRequestWithContext(ctx, "GET", "/no_log", nil)
211+
r.ServeHTTP(res2, req2)
212+
213+
if res2.Code != 204 {
214+
t.Fatalf("request /no_log is failed (%d)", res2.Code)
215+
}
216+
217+
if len(utcLoggerObserved.All()) != 1 {
218+
t.Fatalf("Log should be 1 line but there're %d", len(utcLoggerObserved.All()))
219+
}
220+
221+
logLine := utcLoggerObserved.All()[0]
222+
pathStr := logLine.Context[2].String
223+
if pathStr != testPath {
224+
t.Fatalf("logged path should be /test but %s", pathStr)
225+
}
226+
}

0 commit comments

Comments
 (0)