Skip to content

Commit

Permalink
refactor: refactor logger options to support multiple skip path regexps
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
appleboy committed Nov 28, 2023
1 parent b791254 commit 31e8dd5
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 21 deletions.
2 changes: 1 addition & 1 deletion _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
})
Expand Down
6 changes: 2 additions & 4 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,13 @@ 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")
assert.Contains(t, buffer.String(), "WRN")

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")
Expand Down Expand Up @@ -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")
Expand Down
19 changes: 3 additions & 16 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
})
}

Expand Down

0 comments on commit 31e8dd5

Please sign in to comment.