Skip to content

Commit

Permalink
docs(fields): add custom fields example
Browse files Browse the repository at this point in the history
  • Loading branch information
appleboy committed Sep 15, 2022
1 parent 81070e0 commit 51843d4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,50 @@ This will add the `traceId` field to log:
"traceID": "285f31ec1dba4b79034c4415ad18e4ed"
}
```

## Custom Zap fields

example for custom log request body and response request ID

```go
func main() {
r := gin.New()

logger, _ := zap.NewProduction()

r.Use(ginzap.GinzapWithConfig(logger, &ginzap.Config{
UTC: true,
TimeFormat: time.RFC3339,
Context: ginzap.Fn(func(c *gin.Context) (fields []zapcore.Field) {
// log response ID
if requestID := c.Writer.Header().Get("X-Request-Id"); requestID != "" {
fields = append(fields, zap.String("request-id", requestID))
}

// log request body
var body []byte
var buf bytes.Buffer
tee := io.TeeReader(c.Request.Body, &buf)
body, _ = io.ReadAll(tee)
c.Request.Body = io.NopCloser(&buf)
fields = append(fields, zap.String("body", string(body)))

return
}),
}))

// 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
r.Run(":8080")
}
```
File renamed without changes.
54 changes: 54 additions & 0 deletions _example/example02/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"bytes"
"fmt"
"io"
"time"

ginzap "github.com/gin-contrib/zap"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func main() {
r := gin.New()

logger, _ := zap.NewProduction()

r.Use(ginzap.GinzapWithConfig(logger, &ginzap.Config{
UTC: true,
TimeFormat: time.RFC3339,
Context: ginzap.Fn(func(c *gin.Context) (fields []zapcore.Field) {
// log response ID
if requestID := c.Writer.Header().Get("X-Request-Id"); requestID != "" {
fields = append(fields, zap.String("request-id", requestID))
}

// log request body
var body []byte
var buf bytes.Buffer
tee := io.TeeReader(c.Request.Body, &buf)
body, _ = io.ReadAll(tee)
c.Request.Body = io.NopCloser(&buf)
fields = append(fields, zap.String("body", string(body)))

return
}),
}))

// 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
r.Run(":8080")
}

0 comments on commit 51843d4

Please sign in to comment.