Skip to content

Commit

Permalink
feat: consolidate code examples into new file
Browse files Browse the repository at this point in the history
- 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 <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Mar 2, 2024
1 parent a9803c5 commit 58dd173
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 4 deletions.
63 changes: 61 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}
```
4 changes: 3 additions & 1 deletion _example/example01/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
4 changes: 3 additions & 1 deletion _example/example02/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
41 changes: 41 additions & 0 deletions _example/example03/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 58dd173

Please sign in to comment.