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: add skip regexp paths #61

Closed
Closed
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ When you want to skip logging for specific path,
please use GinzapWithConfig

```go
regexp1 := regexp.MustCompile(`^/regexp\d*`)

r.Use(GinzapWithConfig(utcLogger, &Config{
TimeFormat: time.RFC3339,
UTC: true,
SkipPaths: []string{"/no_log"},
SkipRegexpPaths: []*regexp.Regexp{regexp1},
}))
```

Expand Down
77 changes: 46 additions & 31 deletions zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httputil"
"os"
"regexp"
"runtime/debug"
"strings"
"time"
Expand All @@ -26,10 +27,11 @@ type ZapLogger interface {

// Config is config setting for Ginzap
type Config struct {
TimeFormat string
UTC bool
SkipPaths []string
Context Fn
TimeFormat string
UTC bool
SkipPaths []string
SkipRegexpPaths []*regexp.Regexp
Context Fn
}

// Ginzap returns a gin.HandlerFunc (middleware) that logs requests using uber-go/zap.
Expand Down Expand Up @@ -58,38 +60,51 @@ func GinzapWithConfig(logger ZapLogger, conf *Config) gin.HandlerFunc {
query := c.Request.URL.RawQuery
c.Next()

if _, ok := skipPaths[path]; !ok {
end := time.Now()
latency := end.Sub(start)
if conf.UTC {
end = end.UTC()
}
var track = true
if _, ok := skipPaths[path]; ok {
track = false
}

fields := []zapcore.Field{
zap.Int("status", c.Writer.Status()),
zap.String("method", c.Request.Method),
zap.String("path", path),
zap.String("query", query),
zap.String("ip", c.ClientIP()),
zap.String("user-agent", c.Request.UserAgent()),
zap.Duration("latency", latency),
}
if conf.TimeFormat != "" {
fields = append(fields, zap.String("time", end.Format(conf.TimeFormat)))
for _, reg := range conf.SkipRegexpPaths {
if reg.MatchString(path) {
track = false
}
}

if conf.Context != nil {
fields = append(fields, conf.Context(c)...)
}
if !track {
return
}

if len(c.Errors) > 0 {
// Append error field if this is an erroneous request.
for _, e := range c.Errors.Errors() {
logger.Error(e, fields...)
}
} else {
logger.Info(path, fields...)
end := time.Now()
latency := end.Sub(start)
if conf.UTC {
end = end.UTC()
}

fields := []zapcore.Field{
zap.Int("status", c.Writer.Status()),
zap.String("method", c.Request.Method),
zap.String("path", path),
zap.String("query", query),
zap.String("ip", c.ClientIP()),
zap.String("user-agent", c.Request.UserAgent()),
zap.Duration("latency", latency),
}
if conf.TimeFormat != "" {
fields = append(fields, zap.String("time", end.Format(conf.TimeFormat)))
}

if conf.Context != nil {
fields = append(fields, conf.Context(c)...)
}

if len(c.Errors) > 0 {
// Append error field if this is an erroneous request.
for _, e := range c.Errors.Errors() {
logger.Error(e, fields...)
}
} else {
logger.Info(path, fields...)
}
}
}
Expand Down