diff --git a/README.md b/README.md index 70b2c84..25a860f 100644 --- a/README.md +++ b/README.md @@ -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}, })) ``` diff --git a/zap.go b/zap.go index 6e8d748..bc4386c 100644 --- a/zap.go +++ b/zap.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httputil" "os" + "regexp" "runtime/debug" "strings" "time" @@ -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. @@ -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...) } } }