Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(regex): add multi skip regexp path #66

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ type Fn func(*gin.Context, zerolog.Logger) zerolog.Logger
type config struct {
logger Fn
// UTC a boolean stating whether to use UTC time zone or local.
utc bool
skipPath []string
skipPathRegexp *regexp.Regexp
utc bool
skipPath []string
skipPathRegexps []*regexp.Regexp
// Output is a writer where logs are written.
// Optional. Default value is gin.DefaultWriter.
output io.Writer
Expand Down Expand Up @@ -87,10 +87,15 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
track = false
}

if track &&
cfg.skipPathRegexp != nil &&
cfg.skipPathRegexp.MatchString(path) {
track = false
if track && len(cfg.skipPathRegexps) > 0 {
for _, reg := range cfg.skipPathRegexps {
if !reg.MatchString(path) {
continue
}

track = false
break
}
}

if track {
Expand Down
21 changes: 19 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,26 @@ func WithLogger(fn func(*gin.Context, zerolog.Logger) zerolog.Logger) Option {
}

// WithSkipPathRegexp skip URL path by regexp pattern
func WithSkipPathRegexp(s *regexp.Regexp) Option {
func WithSkipPathRegexp(reg *regexp.Regexp) Option {
return optionFunc(func(c *config) {
c.skipPathRegexp = s
if reg == nil {
return
}

c.skipPathRegexps = append(c.skipPathRegexps, reg)
})
}

// WithSkipPathRegexps multiple skip URL paths by regexp pattern
func WithSkipPathRegexps(regs []*regexp.Regexp) Option {
return optionFunc(func(c *config) {
if regs == nil {
return
}

for _, reg := range regs {
c.skipPathRegexps = append(c.skipPathRegexps, reg)
}
})
}

Expand Down