Skip to content

Commit

Permalink
feat: 优化Tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
2637309949 committed Oct 23, 2020
1 parent 0bd6cf1 commit 64a18e5
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions platform/plugin/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,21 @@ func Tracker(receiver ...func(*gin.Context, *LogFormatterParams)) gin.HandlerFun

return func(c *gin.Context) {
// bytes buffer
buf, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
panic(err)
var buf []byte
var err error
if cb, ok := c.Get(gin.BodyBytesKey); ok {
if cbb, ok := cb.([]byte); ok {
buf = cbb
}
}
if buf == nil {
buf, err = ioutil.ReadAll(c.Request.Body)
if err != nil {
panic(err)
}
c.Set(gin.BodyBytesKey, buf)
}

c.Request.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(buf)))
writer := &bodyLogWriter{body: bytes.NewBufferString(""), ResponseWriter: c.Writer}
c.Writer = writer
Expand Down

0 comments on commit 64a18e5

Please sign in to comment.