添加example&优化参数截取
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/yuninks/loggerx"
|
||||
"github.com/yuninks/loggerx/middleware"
|
||||
)
|
||||
|
||||
// curl --location '127.0.0.1:8080/ping'
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
log := loggerx.NewLogger(ctx, loggerx.SetToConsole())
|
||||
|
||||
g := gin.Default()
|
||||
|
||||
g.Use(middleware.SetGinTraceIdByLogger(log))
|
||||
g.Use(middleware.SetGinParams(log))
|
||||
|
||||
g.GET("/ping", func(ctx *gin.Context) {
|
||||
log.Infof(ctx, "GET /ping")
|
||||
ctx.JSON(200, gin.H{"message": "pong"})
|
||||
})
|
||||
|
||||
g.Run(":8080")
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/yuninks/loggerx"
|
||||
)
|
||||
@@ -11,6 +12,8 @@ func main() {
|
||||
log := loggerx.NewLogger(ctx,
|
||||
// loggerx.SetPrintFile(false),
|
||||
loggerx.SetToConsole(),
|
||||
// loggerx.SetTimeZone(time.UTC),
|
||||
loggerx.SetTimeZone(time.FixedZone("CST", 8*3600)),
|
||||
)
|
||||
log.Info(ctx, "哈哈哈2")
|
||||
log.Info(ctx, "哈哈哈2")
|
||||
|
||||
+18
-6
@@ -18,6 +18,12 @@ import (
|
||||
func SetGinParams(log *loggerx.Logger) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
|
||||
reqBody := "非json不保存Body"
|
||||
|
||||
// 请求参数
|
||||
reqContentType := ctx.Request.Header.Get("Content-Type")
|
||||
if strings.Contains(reqContentType, "application/json") {
|
||||
|
||||
// 使用bytes.Buffer来读取并记录请求体,同时避免改变ctx.Request.Body
|
||||
buf := &bytes.Buffer{}
|
||||
tea := io.TeeReader(ctx.Request.Body, buf)
|
||||
@@ -31,14 +37,13 @@ func SetGinParams(log *loggerx.Logger) gin.HandlerFunc {
|
||||
}
|
||||
|
||||
// 截取body的前1000个字符
|
||||
bodyStr := string(body)
|
||||
if len(bodyStr) > 1000 {
|
||||
bodyStr = bodyStr[:1000]
|
||||
}
|
||||
reqBody = getLimitBody(body)
|
||||
|
||||
// 使用NopCloser包裹buffer,仅为了确保在读取body之后body可以被关闭,但并不改变原始的Request.Body
|
||||
ctx.Request.Body = io.NopCloser(buf)
|
||||
|
||||
}
|
||||
|
||||
// 下面是响应参数缓存
|
||||
blw := &bodyParamsWriter{
|
||||
body: bytes.NewBufferString(""),
|
||||
@@ -56,7 +61,7 @@ func SetGinParams(log *loggerx.Logger) gin.HandlerFunc {
|
||||
Method: ctx.Request.Method,
|
||||
Path: ctx.FullPath(),
|
||||
ClientIP: ctx.ClientIP(),
|
||||
Body: bodyStr,
|
||||
Body: reqBody,
|
||||
Header: ctx.Request.Header,
|
||||
}
|
||||
|
||||
@@ -71,7 +76,7 @@ func SetGinParams(log *loggerx.Logger) gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
respData := blw.body.String()
|
||||
respData := getLimitBody(blw.body.Bytes())
|
||||
|
||||
resp := struct {
|
||||
HttpCode int `json:"http_code"`
|
||||
@@ -97,3 +102,10 @@ func (w bodyParamsWriter) Write(b []byte) (int, error) {
|
||||
w.body.Write(b)
|
||||
return w.ResponseWriter.Write(b)
|
||||
}
|
||||
|
||||
func getLimitBody(body []byte) string {
|
||||
if len(body) > 1000 {
|
||||
return string(body[:1000])
|
||||
}
|
||||
return string(body)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user