From 31e8dd535e64d95a184da70f40e2f2254dca6401 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 28 Nov 2023 10:33:19 +0800 Subject: [PATCH] refactor: refactor logger options to support multiple skip path regexps - Modify the `main.go` file by replacing `logger.WithSkipPathRegexp(rxURL)` with `logger.WithSkipPathRegexps(rxURL)` - Remove the line `assert.Equal(t, 400, resp.Code)` from the `logger_test.go` file - Remove the line `assert.Equal(t, 502, resp.Code)` from the `logger_test.go` file - Modify the `logger_test.go` file by replacing `WithSkipPathRegexp(rxURL)` with `WithSkipPathRegexps(rxURL)` - Modify the `logger_test.go` file by replacing `WithSkipPathRegexp(rxURL)` with `WithSkipPathRegexps(rxURL)` - Remove the function `WithSkipPathRegexp(reg *regexp.Regexp)` from the `options.go` file - Modify the function `WithSkipPathRegexps(regs []*regexp.Regexp)` in the `options.go` file by changing the parameter to `regs ...*regexp.Regexp` - Modify the function `WithSkipPathRegexps(regs []*regexp.Regexp)` in the `options.go` file by changing the parameter to `regs ...*regexp.Regexp` - Modify the function `WithSkipPathRegexps(regs []*regexp.Regexp)` in the `options.go` file by changing the parameter to `regs ...*regexp.Regexp` Signed-off-by: Bo-Yi Wu --- _example/main.go | 2 +- logger_test.go | 6 ++---- options.go | 19 +++---------------- 3 files changed, 6 insertions(+), 21 deletions(-) diff --git a/_example/main.go b/_example/main.go index 888e2a2..28da35d 100644 --- a/_example/main.go +++ b/_example/main.go @@ -33,7 +33,7 @@ func main() { r.GET("/ping", logger.SetLogger( logger.WithSkipPath([]string{"/skip"}), logger.WithUTC(true), - logger.WithSkipPathRegexp(rxURL), + logger.WithSkipPathRegexps(rxURL), ), func(c *gin.Context) { c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix())) }) diff --git a/logger_test.go b/logger_test.go index 4173c89..ccc332d 100644 --- a/logger_test.go +++ b/logger_test.go @@ -53,7 +53,6 @@ func TestLogger(t *testing.T) { buffer.Reset() performRequest(r, "POST", "/example?a=100") - assert.Equal(t, 400, resp.Code) assert.Contains(t, buffer.String(), "400") assert.Contains(t, buffer.String(), "POST") assert.Contains(t, buffer.String(), "/example") @@ -61,7 +60,6 @@ func TestLogger(t *testing.T) { buffer.Reset() performRequest(r, "PUT", "/example?a=100") - assert.Equal(t, 502, resp.Code) assert.Contains(t, buffer.String(), "502") assert.Contains(t, buffer.String(), "PUT") assert.Contains(t, buffer.String(), "/example") @@ -92,12 +90,12 @@ func TestLoggerWithLogger(t *testing.T) { r.GET("/regexp01", SetLogger( WithWriter(buffer), - WithSkipPathRegexp(rxURL), + WithSkipPathRegexps(rxURL), ), func(c *gin.Context) {}) r.GET("/regexp02", SetLogger( WithWriter(buffer), - WithSkipPathRegexp(rxURL), + WithSkipPathRegexps(rxURL), ), func(c *gin.Context) {}) performRequest(r, "GET", "/example?a=100") diff --git a/options.go b/options.go index 4705537..2906586 100644 --- a/options.go +++ b/options.go @@ -28,27 +28,14 @@ func WithLogger(fn func(*gin.Context, zerolog.Logger) zerolog.Logger) Option { }) } -// WithSkipPathRegexp skip URL path by regexp pattern -func WithSkipPathRegexp(reg *regexp.Regexp) Option { - return optionFunc(func(c *config) { - if reg == nil { - return - } - - c.skipPathRegexps = append(c.skipPathRegexps, reg) - }) -} - // WithSkipPathRegexps multiple skip URL paths by regexp pattern -func WithSkipPathRegexps(regs []*regexp.Regexp) Option { +func WithSkipPathRegexps(regs ...*regexp.Regexp) Option { return optionFunc(func(c *config) { - if regs == nil { + if len(regs) == 0 { return } - for _, reg := range regs { - c.skipPathRegexps = append(c.skipPathRegexps, reg) - } + c.skipPathRegexps = append(c.skipPathRegexps, regs...) }) }