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: implement skip path regexps feature in zap package #72

Merged
merged 1 commit into from
Mar 2, 2024
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
23 changes: 18 additions & 5 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 @@ -29,11 +30,12 @@ type ZapLogger interface {

// Config is config setting for Ginzap
type Config struct {
TimeFormat string
UTC bool
SkipPaths []string
Context Fn
DefaultLevel zapcore.Level
TimeFormat string
UTC bool
SkipPaths []string
SkipPathRegexps []*regexp.Regexp
Context Fn
DefaultLevel zapcore.Level
// skip is a Skipper that indicates which logs should not be written.
// Optional.
Skipper Skipper
Expand Down Expand Up @@ -70,6 +72,17 @@ func GinzapWithConfig(logger ZapLogger, conf *Config) gin.HandlerFunc {
track = false
}

if track && len(conf.SkipPathRegexps) > 0 {
for _, reg := range conf.SkipPathRegexps {
if !reg.MatchString(path) {
continue
}

track = false
break
}
}

if track {
end := time.Now()
latency := end.Sub(start)
Expand Down
46 changes: 46 additions & 0 deletions zap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"regexp"
"testing"
"time"

Expand Down Expand Up @@ -178,3 +179,48 @@ func TestLoggerSkipper(t *testing.T) {
t.Fatalf("logged path should be /test but %s", pathStr)
}
}

func TestSkipPathRegexps(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
r := gin.New()

rxURL := regexp.MustCompile(`^/no_\s*`)

utcLogger, utcLoggerObserved := buildDummyLogger()
r.Use(GinzapWithConfig(utcLogger, &Config{
TimeFormat: time.RFC3339,
UTC: true,
SkipPathRegexps: []*regexp.Regexp{rxURL},
}))

r.GET(testPath, func(c *gin.Context) {
c.JSON(204, nil)
})

r.GET("/no_log", func(c *gin.Context) {
c.JSON(204, nil)
})

res1 := httptest.NewRecorder()
req1, _ := http.NewRequestWithContext(ctx, "GET", testPath, nil)
r.ServeHTTP(res1, req1)

res2 := httptest.NewRecorder()
req2, _ := http.NewRequestWithContext(ctx, "GET", "/no_log", nil)
r.ServeHTTP(res2, req2)

if res2.Code != 204 {
t.Fatalf("request /no_log is failed (%d)", res2.Code)
}

if len(utcLoggerObserved.All()) != 1 {
t.Fatalf("Log should be 1 line but there're %d", len(utcLoggerObserved.All()))
}

logLine := utcLoggerObserved.All()[0]
pathStr := logLine.Context[2].String
if pathStr != testPath {
t.Fatalf("logged path should be /test but %s", pathStr)
}
}
Loading