perf👌 : logger 添加传入传出项
This commit is contained in:
1 parent
abe9c42aff
commit
98d30f64f6
1 file changed
+30
-21
@@ -1,6 +1,7 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -9,7 +10,6 @@ import (
|
|||||||
"go-admin/app/admin/models"
|
"go-admin/app/admin/models"
|
||||||
"go-admin/app/admin/models/system"
|
"go-admin/app/admin/models/system"
|
||||||
"go-admin/app/admin/service"
|
"go-admin/app/admin/service"
|
||||||
"go-admin/common/global"
|
|
||||||
"go-admin/common/log"
|
"go-admin/common/log"
|
||||||
"go-admin/tools"
|
"go-admin/tools"
|
||||||
"go-admin/tools/config"
|
"go-admin/tools/config"
|
||||||
@@ -17,32 +17,42 @@ import (
|
|||||||
|
|
||||||
// LoggerToFile 日志记录到文件
|
// LoggerToFile 日志记录到文件
|
||||||
func LoggerToFile() gin.HandlerFunc {
|
func LoggerToFile() gin.HandlerFunc {
|
||||||
|
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
// 开始时间
|
// 开始时间
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
|
|
||||||
// 处理请求
|
// 处理请求
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|
||||||
// 结束时间
|
bd, bl := c.Get("body")
|
||||||
endTime := time.Now()
|
var body = ""
|
||||||
|
if bl {
|
||||||
|
body = bd.(string)
|
||||||
|
}
|
||||||
|
|
||||||
// 执行时间
|
rt, bl := c.Get("result")
|
||||||
latencyTime := endTime.Sub(startTime)
|
var result = ""
|
||||||
|
if bl {
|
||||||
|
result = rt.(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
st, bl := c.Get("status")
|
||||||
|
var statusBus = 0
|
||||||
|
if bl {
|
||||||
|
statusBus = st.(int)
|
||||||
|
}
|
||||||
|
|
||||||
// 请求方式
|
// 请求方式
|
||||||
reqMethod := c.Request.Method
|
reqMethod := c.Request.Method
|
||||||
|
|
||||||
// 请求路由
|
// 请求路由
|
||||||
reqUri := c.Request.RequestURI
|
reqUri := c.Request.RequestURI
|
||||||
|
|
||||||
// 状态码
|
// 状态码
|
||||||
statusCode := c.Writer.Status()
|
statusCode := c.Writer.Status()
|
||||||
|
|
||||||
// 请求IP
|
// 请求IP
|
||||||
clientIP := c.ClientIP()
|
clientIP := c.ClientIP()
|
||||||
|
// 结束时间
|
||||||
|
endTime := time.Now()
|
||||||
|
// 执行时间
|
||||||
|
latencyTime := endTime.Sub(startTime)
|
||||||
// 日志格式
|
// 日志格式
|
||||||
logData := map[string]interface{}{
|
logData := map[string]interface{}{
|
||||||
"statusCode": statusCode,
|
"statusCode": statusCode,
|
||||||
@@ -52,16 +62,14 @@ func LoggerToFile() gin.HandlerFunc {
|
|||||||
"uri": reqUri,
|
"uri": reqUri,
|
||||||
}
|
}
|
||||||
log.Info(logData)
|
log.Info(logData)
|
||||||
global.RequestLogger.Info(logData)
|
|
||||||
|
|
||||||
if c.Request.Method != "GET" && c.Request.Method != "OPTIONS" && config.LoggerConfig.EnabledDB {
|
if c.Request.Method != "GET" && c.Request.Method != "OPTIONS" && config.LoggerConfig.EnabledDB {
|
||||||
SetDBOperLog(c, clientIP, statusCode, reqUri, reqMethod, latencyTime)
|
SetDBOperLog(c, clientIP, statusCode, reqUri, reqMethod, latencyTime, body, result, statusBus)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetDBOperLog 写入操作日志表 fixme 该方法后续即将弃用
|
// SetDBOperLog 写入操作日志表 fixme 该方法后续即将弃用
|
||||||
func SetDBOperLog(c *gin.Context, clientIP string, statusCode int, reqUri string, reqMethod string, latencyTime time.Duration) {
|
func SetDBOperLog(c *gin.Context, clientIP string, statusCode int, reqUri string, reqMethod string, latencyTime time.Duration, body string, result string, status int) {
|
||||||
menu := models.Menu{}
|
menu := models.Menu{}
|
||||||
menu.Path = reqUri
|
menu.Path = reqUri
|
||||||
menu.Action = reqMethod
|
menu.Action = reqMethod
|
||||||
@@ -73,6 +81,7 @@ func SetDBOperLog(c *gin.Context, clientIP string, statusCode int, reqUri string
|
|||||||
sysOperaLog.OperName = tools.GetUserName(c)
|
sysOperaLog.OperName = tools.GetUserName(c)
|
||||||
sysOperaLog.RequestMethod = c.Request.Method
|
sysOperaLog.RequestMethod = c.Request.Method
|
||||||
sysOperaLog.OperUrl = reqUri
|
sysOperaLog.OperUrl = reqUri
|
||||||
|
sysOperaLog.OperParam = body
|
||||||
if reqUri == "/login" {
|
if reqUri == "/login" {
|
||||||
sysOperaLog.BusinessType = "10"
|
sysOperaLog.BusinessType = "10"
|
||||||
sysOperaLog.Title = "用户登录"
|
sysOperaLog.Title = "用户登录"
|
||||||
@@ -95,14 +104,14 @@ func SetDBOperLog(c *gin.Context, clientIP string, statusCode int, reqUri string
|
|||||||
if len(menuList) > 0 {
|
if len(menuList) > 0 {
|
||||||
sysOperaLog.Title = menuList[0].Title
|
sysOperaLog.Title = menuList[0].Title
|
||||||
}
|
}
|
||||||
b, _ := c.Get("body")
|
|
||||||
sysOperaLog.OperParam, _ = tools.StructToJsonStr(b)
|
|
||||||
sysOperaLog.CreateBy = tools.GetUserId(c)
|
sysOperaLog.CreateBy = tools.GetUserId(c)
|
||||||
sysOperaLog.OperTime = tools.GetCurrentTime()
|
sysOperaLog.OperTime = tools.GetCurrentTime()
|
||||||
sysOperaLog.LatencyTime = (latencyTime).String()
|
sysOperaLog.LatencyTime = fmt.Sprintf("%v", latencyTime)
|
||||||
|
|
||||||
|
sysOperaLog.JsonResult = result
|
||||||
sysOperaLog.UserAgent = c.Request.UserAgent()
|
sysOperaLog.UserAgent = c.Request.UserAgent()
|
||||||
if c.Err() == nil {
|
if status == 200 {
|
||||||
sysOperaLog.Status = "0"
|
sysOperaLog.Status = "2"
|
||||||
} else {
|
} else {
|
||||||
sysOperaLog.Status = "1"
|
sysOperaLog.Status = "1"
|
||||||
}
|
}
|
||||||
@@ -113,5 +122,5 @@ func SetDBOperLog(c *gin.Context, clientIP string, statusCode int, reqUri string
|
|||||||
}
|
}
|
||||||
serviceOperaLog := service.SysOperaLog{}
|
serviceOperaLog := service.SysOperaLog{}
|
||||||
serviceOperaLog.Orm = db
|
serviceOperaLog.Orm = db
|
||||||
_ = serviceOperaLog.InsertSysOperaLog(sysOperaLog.Generate())
|
_ = serviceOperaLog.InsertSysOperaLog(&sysOperaLog)
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user