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) + } +}