Skip to content

Commit

Permalink
docs: improve documentation and add example code
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
appleboy committed Mar 2, 2024
1 parent 5b62309 commit ca35b5f
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
```
35 changes: 35 additions & 0 deletions _example/example04/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit ca35b5f

Please sign in to comment.