添加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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/yuninks/loggerx"
|
"github.com/yuninks/loggerx"
|
||||||
)
|
)
|
||||||
@@ -11,6 +12,8 @@ func main() {
|
|||||||
log := loggerx.NewLogger(ctx,
|
log := loggerx.NewLogger(ctx,
|
||||||
// loggerx.SetPrintFile(false),
|
// loggerx.SetPrintFile(false),
|
||||||
loggerx.SetToConsole(),
|
loggerx.SetToConsole(),
|
||||||
|
// loggerx.SetTimeZone(time.UTC),
|
||||||
|
loggerx.SetTimeZone(time.FixedZone("CST", 8*3600)),
|
||||||
)
|
)
|
||||||
log.Info(ctx, "哈哈哈2")
|
log.Info(ctx, "哈哈哈2")
|
||||||
log.Info(ctx, "哈哈哈2")
|
log.Info(ctx, "哈哈哈2")
|
||||||
|
|||||||
+32
-20
@@ -18,27 +18,32 @@ import (
|
|||||||
func SetGinParams(log *loggerx.Logger) gin.HandlerFunc {
|
func SetGinParams(log *loggerx.Logger) gin.HandlerFunc {
|
||||||
return func(ctx *gin.Context) {
|
return func(ctx *gin.Context) {
|
||||||
|
|
||||||
// 使用bytes.Buffer来读取并记录请求体,同时避免改变ctx.Request.Body
|
reqBody := "非json不保存Body"
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
tea := io.TeeReader(ctx.Request.Body, buf)
|
// 请求参数
|
||||||
|
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)
|
||||||
|
|
||||||
|
// 读取body
|
||||||
|
body, err := io.ReadAll(tea)
|
||||||
|
if err != nil {
|
||||||
|
log.Infof(ctx, "Error reading request body: %v", err)
|
||||||
|
ctx.AbortWithStatus(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 截取body的前1000个字符
|
||||||
|
reqBody = getLimitBody(body)
|
||||||
|
|
||||||
|
// 使用NopCloser包裹buffer,仅为了确保在读取body之后body可以被关闭,但并不改变原始的Request.Body
|
||||||
|
ctx.Request.Body = io.NopCloser(buf)
|
||||||
|
|
||||||
// 读取body
|
|
||||||
body, err := io.ReadAll(tea)
|
|
||||||
if err != nil {
|
|
||||||
log.Infof(ctx, "Error reading request body: %v", err)
|
|
||||||
ctx.AbortWithStatus(http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 截取body的前1000个字符
|
|
||||||
bodyStr := string(body)
|
|
||||||
if len(bodyStr) > 1000 {
|
|
||||||
bodyStr = bodyStr[:1000]
|
|
||||||
}
|
|
||||||
|
|
||||||
// 使用NopCloser包裹buffer,仅为了确保在读取body之后body可以被关闭,但并不改变原始的Request.Body
|
|
||||||
ctx.Request.Body = io.NopCloser(buf)
|
|
||||||
|
|
||||||
// 下面是响应参数缓存
|
// 下面是响应参数缓存
|
||||||
blw := &bodyParamsWriter{
|
blw := &bodyParamsWriter{
|
||||||
body: bytes.NewBufferString(""),
|
body: bytes.NewBufferString(""),
|
||||||
@@ -56,7 +61,7 @@ func SetGinParams(log *loggerx.Logger) gin.HandlerFunc {
|
|||||||
Method: ctx.Request.Method,
|
Method: ctx.Request.Method,
|
||||||
Path: ctx.FullPath(),
|
Path: ctx.FullPath(),
|
||||||
ClientIP: ctx.ClientIP(),
|
ClientIP: ctx.ClientIP(),
|
||||||
Body: bodyStr,
|
Body: reqBody,
|
||||||
Header: ctx.Request.Header,
|
Header: ctx.Request.Header,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,7 +76,7 @@ func SetGinParams(log *loggerx.Logger) gin.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
respData := blw.body.String()
|
respData := getLimitBody(blw.body.Bytes())
|
||||||
|
|
||||||
resp := struct {
|
resp := struct {
|
||||||
HttpCode int `json:"http_code"`
|
HttpCode int `json:"http_code"`
|
||||||
@@ -97,3 +102,10 @@ func (w bodyParamsWriter) Write(b []byte) (int, error) {
|
|||||||
w.body.Write(b)
|
w.body.Write(b)
|
||||||
return w.ResponseWriter.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