feat ✨ 日志中间件调整
This commit is contained in:
1 parent
b902ab2a5f
commit
1ac6568f67
1 file changed
+20
-7
@@ -1,7 +1,11 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -19,9 +23,24 @@ import (
|
|||||||
// LoggerToFile 日志记录到文件
|
// LoggerToFile 日志记录到文件
|
||||||
func LoggerToFile() gin.HandlerFunc {
|
func LoggerToFile() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
|
log := api.GetRequestLogger(c)
|
||||||
// 开始时间
|
// 开始时间
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
// 处理请求
|
// 处理请求
|
||||||
|
var body string
|
||||||
|
switch c.Request.Method {
|
||||||
|
case http.MethodPost, http.MethodPut, http.MethodDelete:
|
||||||
|
bf := bytes.NewBuffer(nil)
|
||||||
|
wt := bufio.NewWriter(bf)
|
||||||
|
_, err := io.Copy(wt, c.Request.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("copy body error, %s", err.Error())
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
rb, _ := ioutil.ReadAll(bf)
|
||||||
|
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(rb))
|
||||||
|
body =string(rb)
|
||||||
|
}
|
||||||
|
|
||||||
c.Next()
|
c.Next()
|
||||||
url := c.Request.RequestURI
|
url := c.Request.RequestURI
|
||||||
@@ -34,13 +53,7 @@ func LoggerToFile() gin.HandlerFunc {
|
|||||||
if c.Request.Method == http.MethodOptions {
|
if c.Request.Method == http.MethodOptions {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log := api.GetRequestLogger(c)
|
|
||||||
|
|
||||||
bd, bl := c.Get("body")
|
|
||||||
var body = ""
|
|
||||||
if bl {
|
|
||||||
body = bd.(string)
|
|
||||||
}
|
|
||||||
|
|
||||||
rt, bl := c.Get("result")
|
rt, bl := c.Get("result")
|
||||||
var result = ""
|
var result = ""
|
||||||
@@ -79,7 +92,7 @@ func LoggerToFile() gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
log.WithFields(logData).Info()
|
log.WithFields(logData).Info()
|
||||||
|
|
||||||
if c.Request.Method != "GET" && c.Request.Method != "OPTIONS" && config.LoggerConfig.EnabledDB {
|
if c.Request.Method != "OPTIONS" && c.Request.Method != "GET" && config.LoggerConfig.EnabledDB {
|
||||||
SetDBOperLog(c, clientIP, statusCode, reqUri, reqMethod, latencyTime, body, result, statusBus)
|
SetDBOperLog(c, clientIP, statusCode, reqUri, reqMethod, latencyTime, body, result, statusBus)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user