Skip to content

Commit

Permalink
feat(func): implement Skipper functionality for logging configuration (
Browse files Browse the repository at this point in the history
…#71)

* feat: implement Skipper functionality for logging configuration

- Add a new type `Skipper` to skip logs based on provided context
- Add a new field `Skipper` to the `Config` struct
- Modify the condition in `GinzapWithConfig` function to include `conf.Skipper` check
- Add a new test function `TestLoggerSkipper` in `zap_test.go`

Signed-off-by: appleboy <appleboy.tw@gmail.com>

* chore: update

Signed-off-by: appleboy <appleboy.tw@gmail.com>

---------

Signed-off-by: appleboy <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Mar 2, 2024
1 parent 6ad826e commit d4400a8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
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)
}
}

0 comments on commit d4400a8

Please sign in to comment.