update method of operation record
This commit is contained in:
@@ -9,56 +9,60 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var body []byte
|
||||
|
||||
func RecordRequestBody() gin.HandlerFunc {
|
||||
func OperationRecord() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var body []byte
|
||||
if c.Request.Method != http.MethodGet {
|
||||
var err error
|
||||
body, err = ioutil.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error(err)
|
||||
global.GVA_LOG.Error("read body from request error:", err)
|
||||
} else {
|
||||
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
|
||||
}
|
||||
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
|
||||
} else {
|
||||
body = nil
|
||||
}
|
||||
userId, err := strconv.Atoi(c.Request.Header.Get("x-user-id"))
|
||||
if err != nil {
|
||||
userId = 0
|
||||
}
|
||||
record := model.SysOperationRecord{
|
||||
Ip: c.ClientIP(),
|
||||
Method: c.Request.Method,
|
||||
Path: c.Request.URL.Path,
|
||||
Agent: c.Request.UserAgent(),
|
||||
Body: string(body),
|
||||
UserId: userId,
|
||||
}
|
||||
writer := responseBodyWriter{
|
||||
ResponseWriter: c.Writer,
|
||||
body: &bytes.Buffer{},
|
||||
}
|
||||
c.Writer = writer
|
||||
now := time.Now()
|
||||
|
||||
c.Next()
|
||||
|
||||
latency := time.Now().Sub(now)
|
||||
record.ErrorMessage = c.Errors.ByType(gin.ErrorTypePrivate).String()
|
||||
record.Status = c.Writer.Status()
|
||||
record.Latency = latency
|
||||
record.Resp = writer.body.String()
|
||||
|
||||
if err := service.CreateSysOperationRecord(record); err != nil {
|
||||
global.GVA_LOG.Error("create operation record error:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func OperationRecord() gin.HandlerFunc {
|
||||
return gin.LoggerWithConfig(gin.LoggerConfig{
|
||||
Formatter: func(param gin.LogFormatterParams) string {
|
||||
// 防止加载查询参数,再次过滤
|
||||
for _, v := range global.GVA_CONFIG.Operation.SkipPaths {
|
||||
if strings.Contains(param.Path, v) {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
userId, err := strconv.Atoi(param.Request.Header.Get("x-user-id"))
|
||||
if err != nil {
|
||||
userId = 0
|
||||
}
|
||||
err = service.CreateSysOperationRecord(model.SysOperationRecord{
|
||||
Ip: param.ClientIP,
|
||||
Method: param.Method,
|
||||
Path: param.Path,
|
||||
Status: param.StatusCode,
|
||||
Latency: param.Latency,
|
||||
Agent: param.Request.UserAgent(),
|
||||
ErrorMessage: param.ErrorMessage,
|
||||
Body: string(body),
|
||||
UserId: userId,
|
||||
})
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error(err)
|
||||
}
|
||||
return ""
|
||||
},
|
||||
Output: nil,
|
||||
SkipPaths: global.GVA_CONFIG.Operation.SkipPaths,
|
||||
})
|
||||
type responseBodyWriter struct {
|
||||
gin.ResponseWriter
|
||||
body *bytes.Buffer
|
||||
}
|
||||
|
||||
func (r responseBodyWriter) Write(b []byte) (int, error) {
|
||||
r.body.Write(b)
|
||||
return r.ResponseWriter.Write(b)
|
||||
}
|
||||
|
Reference in New Issue
Block a user