From 58dd17382f4a974c3bdccf6abe4af94d66554122 Mon Sep 17 00:00:00 2001 From: appleboy Date: Sat, 2 Mar 2024 11:21:20 +0800 Subject: [PATCH] feat: consolidate code examples into new file - Remove an unnecessary line in the README.md file - Add a new file `_example/example03/main.go` - Add the same code to `_example/example03/main.go` as in the README.md file - Update the `_example/example01/main.go` and `_example/example02/main.go` files to match the changes in the README.md file Signed-off-by: appleboy --- README.md | 63 ++++++++++++++++++++++++++++++++++++-- _example/example01/main.go | 4 ++- _example/example02/main.go | 4 ++- _example/example03/main.go | 41 +++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 _example/example03/main.go diff --git a/README.md b/README.md index e44aa0a..2048628 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,6 @@ When you want to skip logging for specific path, please use GinzapWithConfig ```go - r.Use(GinzapWithConfig(utcLogger, &Config{ TimeFormat: time.RFC3339, UTC: true, @@ -137,6 +136,66 @@ func main() { }) // Listen and Server in 0.0.0.0:8080 - r.Run(":8080") + if err := r.Run(":8080"); err != nil { + panic(err) + } +} +``` + +Example for custom `skipper` function + +```go +r.Use(GinzapWithConfig(logger, &Config{ + TimeFormat: time.RFC3339, + UTC: true, + Skipper: func(c *gin.Context) bool { + return c.Request.URL.Path == "/ping" && c.Request.Method == "GET" + }, +})) +``` + +Full example + +```go +package main + +import ( + "fmt" + "time" + + ginzap "github.com/gin-contrib/zap" + + "github.com/gin-gonic/gin" + "go.uber.org/zap" +) + +func main() { + r := gin.New() + + logger, _ := zap.NewProduction() + + r.Use(ginzap.GinzapWithConfig(logger, &ginzap.Config{ + UTC: true, + TimeFormat: time.RFC3339, + Skipper: func(c *gin.Context) bool { + return c.Request.URL.Path == "/ping" && c.Request.Method == "GET" + }, + })) + + // Example ping request. + r.GET("/ping", func(c *gin.Context) { + c.Writer.Header().Add("X-Request-Id", "1234-5678-9012") + c.String(200, "pong "+fmt.Sprint(time.Now().Unix())) + }) + + r.POST("/ping", func(c *gin.Context) { + c.Writer.Header().Add("X-Request-Id", "9012-5678-1234") + 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/example01/main.go b/_example/example01/main.go index faf6774..06c8aa5 100644 --- a/_example/example01/main.go +++ b/_example/example01/main.go @@ -35,5 +35,7 @@ func main() { }) // Listen and Server in 0.0.0.0:8080 - r.Run(":8080") + if err := r.Run(":8080"); err != nil { + panic(err) + } } diff --git a/_example/example02/main.go b/_example/example02/main.go index 0d589b4..8e8e384 100644 --- a/_example/example02/main.go +++ b/_example/example02/main.go @@ -59,5 +59,7 @@ func main() { }) // Listen and Server in 0.0.0.0:8080 - r.Run(":8080") + if err := r.Run(":8080"); err != nil { + panic(err) + } } diff --git a/_example/example03/main.go b/_example/example03/main.go new file mode 100644 index 0000000..e39db1d --- /dev/null +++ b/_example/example03/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "time" + + ginzap "github.com/gin-contrib/zap" + + "github.com/gin-gonic/gin" + "go.uber.org/zap" +) + +func main() { + r := gin.New() + + logger, _ := zap.NewProduction() + + r.Use(ginzap.GinzapWithConfig(logger, &ginzap.Config{ + UTC: true, + TimeFormat: time.RFC3339, + Skipper: func(c *gin.Context) bool { + return c.Request.URL.Path == "/ping" && c.Request.Method == "GET" + }, + })) + + // Example ping request. + r.GET("/ping", func(c *gin.Context) { + c.Writer.Header().Add("X-Request-Id", "1234-5678-9012") + c.String(200, "pong "+fmt.Sprint(time.Now().Unix())) + }) + + r.POST("/ping", func(c *gin.Context) { + c.Writer.Header().Add("X-Request-Id", "9012-5678-1234") + 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) + } +}