From ca35b5f8b1279bb0ba0cb4bbe9a8edc62f693de0 Mon Sep 17 00:00:00 2001 From: appleboy Date: Sat, 2 Mar 2024 12:45:02 +0800 Subject: [PATCH] docs: improve documentation and add example code - Add a custom `SkipPathRegexps` function to the README.md file - Include an example for the custom function in the README.md file - Add a new file `_example/example04/main.go` with similar content to the README.md file Signed-off-by: appleboy --- README.md | 53 ++++++++++++++++++++++++++++++++++++++ _example/example04/main.go | 35 +++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 _example/example04/main.go diff --git a/README.md b/README.md index abfdac2..570c7d5 100644 --- a/README.md +++ b/README.md @@ -201,3 +201,56 @@ func main() { } } ``` + +## Custom `SkipPathRegexps` function + +Example for custom `SkipPathRegexps` function + +```go +rxURL := regexp.MustCompile(`^/ping\s*`) +r.Use(ginzap.GinzapWithConfig(logger, &ginzap.Config{ + UTC: true, + TimeFormat: time.RFC3339, + SkipPathRegexps: []*regexp.Regexp{rxURL}, +})) +``` + +Full example + +```go +package main + +import ( + "fmt" + "regexp" + "time" + + ginzap "github.com/gin-contrib/zap" + + "github.com/gin-gonic/gin" + "go.uber.org/zap" +) + +func main() { + r := gin.New() + + logger, _ := zap.NewProduction() + rxURL := regexp.MustCompile(`^/ping\s*`) + + r.Use(ginzap.GinzapWithConfig(logger, &ginzap.Config{ + UTC: true, + TimeFormat: time.RFC3339, + SkipPathRegexps: []*regexp.Regexp{rxURL}, + })) + + // Example ping request. + r.GET("/ping1234", func(c *gin.Context) { + c.String(200, "pong "+fmt.Sprint(time.Now().Unix())) + }) + + // Listen and Server in 0.0.0.0:8080 + if err := r.Run(":8080"); err != nil { + panic(err) + } +} +``` diff --git a/_example/example04/main.go b/_example/example04/main.go new file mode 100644 index 0000000..023a66e --- /dev/null +++ b/_example/example04/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "regexp" + "time" + + ginzap "github.com/gin-contrib/zap" + + "github.com/gin-gonic/gin" + "go.uber.org/zap" +) + +func main() { + r := gin.New() + + logger, _ := zap.NewProduction() + rxURL := regexp.MustCompile(`^/ping\s*`) + + r.Use(ginzap.GinzapWithConfig(logger, &ginzap.Config{ + UTC: true, + TimeFormat: time.RFC3339, + SkipPathRegexps: []*regexp.Regexp{rxURL}, + })) + + // Example ping request. + r.GET("/ping1234", func(c *gin.Context) { + c.String(200, "pong "+fmt.Sprint(time.Now().Unix())) + }) + + // Listen and Server in 0.0.0.0:8080 + if err := r.Run(":8080"); err != nil { + panic(err) + } +}