feat ✨: 使用runtime cache中的mq功能优化日志存储
perf 👌: 启用mq将日志存储从中间件中移除到消费者
This commit is contained in:
1 parent
2459c877e0
commit
f188b34253
7 files changed
+184
-103
No files matched your search
@@ -2,21 +2,18 @@ package middleware
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-admin-team/go-admin-core/sdk"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/api"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/config"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/pkg"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/pkg/response"
|
||||
|
||||
"go-admin/app/admin/models"
|
||||
"go-admin/app/admin/models/system"
|
||||
"go-admin/app/admin/service"
|
||||
"go-admin/common/global"
|
||||
)
|
||||
|
||||
// LoggerToFile 日志记录到文件
|
||||
@@ -87,60 +84,49 @@ func LoggerToFile() gin.HandlerFunc {
|
||||
// SetDBOperLog 写入操作日志表 fixme 该方法后续即将弃用
|
||||
func SetDBOperLog(c *gin.Context, clientIP string, statusCode int, reqUri string, reqMethod string, latencyTime time.Duration, body string, result string, status int) {
|
||||
log := api.GetRequestLogger(c)
|
||||
db, err := pkg.GetOrm(c)
|
||||
if err != nil {
|
||||
log.Errorf("get db connection error, %s", err.Error())
|
||||
response.Error(c, http.StatusInternalServerError, err, "数据库连接获取失败")
|
||||
return
|
||||
}
|
||||
|
||||
menu := models.Menu{}
|
||||
menu.Path = reqUri
|
||||
menu.Action = reqMethod
|
||||
menuList, _ := menu.Get(db)
|
||||
sysOperaLog := system.SysOperaLog{}
|
||||
sysOperaLog.OperIp = clientIP
|
||||
sysOperaLog.OperLocation = pkg.GetLocation(clientIP)
|
||||
sysOperaLog.Status = pkg.IntToString(statusCode)
|
||||
sysOperaLog.OperName = user.GetUserName(c)
|
||||
sysOperaLog.RequestMethod = c.Request.Method
|
||||
sysOperaLog.OperUrl = reqUri
|
||||
sysOperaLog.OperParam = body
|
||||
l := make(map[string]interface{})
|
||||
l["_fullPath"] = c.FullPath()
|
||||
l["operUrl"] = reqUri
|
||||
l["method"] = reqMethod
|
||||
l["operIp"] = clientIP
|
||||
l["operLocation"] = pkg.GetLocation(clientIP)
|
||||
l["operName"] = user.GetUserName(c)
|
||||
l["requestMethod"] = c.Request.Method
|
||||
l["operParam"] = body
|
||||
l["operTime"] = time.Now()
|
||||
if reqUri == "/login" {
|
||||
sysOperaLog.BusinessType = "10"
|
||||
sysOperaLog.Title = "用户登录"
|
||||
sysOperaLog.OperName = "-"
|
||||
l["businessType"] = "10"
|
||||
l["title"] = "用户登录"
|
||||
l["operName"] = "-"
|
||||
} else if strings.Contains(reqUri, "/api/v1/logout") {
|
||||
sysOperaLog.BusinessType = "11"
|
||||
l["businessType"] = "11"
|
||||
l["title"] = "退出登录"
|
||||
} else if strings.Contains(reqUri, "/api/v1/getCaptcha") {
|
||||
sysOperaLog.BusinessType = "12"
|
||||
sysOperaLog.Title = "验证码"
|
||||
l["businessType"] = "12"
|
||||
l["title"] = "验证码"
|
||||
} else {
|
||||
if reqMethod == "POST" {
|
||||
sysOperaLog.BusinessType = "1"
|
||||
l["businessType"] = "1"
|
||||
} else if reqMethod == "PUT" {
|
||||
sysOperaLog.BusinessType = "2"
|
||||
l["businessType"] = "2"
|
||||
} else if reqMethod == "DELETE" {
|
||||
sysOperaLog.BusinessType = "3"
|
||||
l["businessType"] = "3"
|
||||
}
|
||||
}
|
||||
sysOperaLog.Method = reqMethod
|
||||
if len(menuList) > 0 {
|
||||
sysOperaLog.Title = menuList[0].Title
|
||||
}
|
||||
sysOperaLog.CreateBy = user.GetUserId(c)
|
||||
sysOperaLog.OperTime = pkg.GetCurrentTime()
|
||||
sysOperaLog.LatencyTime = fmt.Sprintf("%v", latencyTime)
|
||||
|
||||
sysOperaLog.JsonResult = result
|
||||
sysOperaLog.UserAgent = c.Request.UserAgent()
|
||||
if status == 200 {
|
||||
sysOperaLog.Status = "2"
|
||||
if status == http.StatusOK {
|
||||
l["status"] = "2"
|
||||
} else {
|
||||
sysOperaLog.Status = "1"
|
||||
l["status"] = "1"
|
||||
}
|
||||
q := sdk.Runtime.GetCachePrefix(c.Request.Host)
|
||||
message, err := sdk.Runtime.GetStreamMessage("", global.OperateLog, l)
|
||||
if err != nil {
|
||||
log.Errorf("GetStreamMessage error, %s", err.Error())
|
||||
//日志报错错误,不中断请求
|
||||
} else {
|
||||
err = q.Append(message)
|
||||
if err != nil {
|
||||
log.Errorf("Append message error, %s", err.Error())
|
||||
}
|
||||
}
|
||||
serviceOperaLog := service.SysOperaLog{}
|
||||
serviceOperaLog.Orm = db
|
||||
serviceOperaLog.Log = log
|
||||
_ = serviceOperaLog.InsertSysOperaLog(&sysOperaLog)
|
||||
}
|
||||
Reference in new issue
Block a user