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(func): implement Skipper functionality for logging configuration #71

Merged
merged 2 commits 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
13 changes: 12 additions & 1 deletion zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (

type Fn func(c *gin.Context) []zapcore.Field

// Skipper is a function to skip logs based on provided Context
type Skipper func(c *gin.Context) bool

// ZapLogger is the minimal logger interface compatible with zap.Logger
type ZapLogger interface {
Info(msg string, fields ...zap.Field)
Expand All @@ -31,6 +34,9 @@ type Config struct {
SkipPaths []string
Context Fn
DefaultLevel zapcore.Level
// skip is a Skipper that indicates which logs should not be written.
// Optional.
Skipper Skipper
}

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

if _, ok := skipPaths[path]; ok || (conf.Skipper != nil && conf.Skipper(c)) {
track = false
}

if _, ok := skipPaths[path]; !ok {
if track {
end := time.Now()
latency := end.Sub(start)
if conf.UTC {
Expand Down
43 changes: 43 additions & 0 deletions zap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,46 @@ func TestGinzapWithConfig(t *testing.T) {
t.Fatalf("log level should be warn but was %s", logLine.Level.String())
}
}

func TestLoggerSkipper(t *testing.T) {
r := gin.New()

utcLogger, utcLoggerObserved := buildDummyLogger()
r.Use(GinzapWithConfig(utcLogger, &Config{
TimeFormat: time.RFC3339,
UTC: true,
Skipper: func(c *gin.Context) bool {
return c.Request.URL.Path == "/no_log"
},
}))

r.GET("/test", 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.NewRequest("GET", "/test", nil)
r.ServeHTTP(res1, req1)

res2 := httptest.NewRecorder()
req2, _ := http.NewRequest("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 != "/test" {
t.Fatalf("logged path should be /test but %s", pathStr)
}
}
Loading