1 Star 0 Fork 0

danlansky/go-library

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
logging.go 1.79 KB
一键复制 编辑 原始数据 按行查看 历史
zhangminghua 提交于 2024-08-20 14:32 . feat:基础工具包
package middleware
import (
"bytes"
"encoding/json"
"fmt"
"gitee.com/danlansky/go-library/logs"
"github.com/gin-gonic/gin"
"github.com/willf/pad"
"go.uber.org/zap"
"io/ioutil"
"time"
)
type bodyLogWriter struct {
gin.ResponseWriter
body *bytes.Buffer
}
func (w bodyLogWriter) Write(b []byte) (int, error) {
w.body.Write(b)
return w.ResponseWriter.Write(b)
}
// Logging is a middleware function that logs the each request.
func Logging() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now().UTC()
// Read the Body content
var bodyBytes []byte
if c.Request.Body != nil {
bodyBytes, _ = ioutil.ReadAll(c.Request.Body)
}
// Restore the io.ReadCloser to its original state
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
// The basic informations.
method := c.Request.Method
ip := c.ClientIP()
//log.Debugf("New request come in, path: %s, Method: %s, body `%s`", path, method, string(bodyBytes))
blw := &bodyLogWriter{
body: bytes.NewBufferString(""),
ResponseWriter: c.Writer,
}
c.Writer = blw
// Continue.
c.Next()
// Calculates the latency.
end := time.Now().UTC()
latency := end.Sub(start)
var code int
var message string
// 移除json不必要的空格,即json字符串压缩
var postJsonStr bytes.Buffer
_ = json.Compact(&postJsonStr, bodyBytes)
// get code and message
if json.Valid(blw.body.Bytes()) == false {
code = 500
message = "err response,not json"
} else {
code = 200
message = "ok"
}
str := fmt.Sprintf("%s|%s|%s|%d|%s|{code: %d, message: %s}", latency, ip, pad.Right(method, 5, ""), blw.ResponseWriter.Status(), c.Request.URL.RequestURI(), code, message)
logs.AccessLog(str, zap.String("reqid", c.GetString("X-Request-Id")), zap.String("post", postJsonStr.String()))
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/danlansky/go-library.git
git@gitee.com:danlansky/go-library.git
danlansky
go-library
go-library
v1.0.0

搜索帮助