feat: add global & logger
This commit is contained in:
1 parent
7db16d04dd
commit
b77f4b5a1b
43 files changed
+200
-181
No files matched your search
+55
-52
@@ -2,16 +2,14 @@ package middleware
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"go-admin/models"
|
||||
"go-admin/tools"
|
||||
config2 "go-admin/tools/config"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
//实例化
|
||||
var logger = logrus.New()
|
||||
|
||||
// 日志记录到文件
|
||||
func LoggerToFile() gin.HandlerFunc {
|
||||
|
||||
@@ -41,9 +39,8 @@ func LoggerToFile() gin.HandlerFunc {
|
||||
clientIP := c.ClientIP()
|
||||
|
||||
// 日志格式
|
||||
logger.Infof("%s [%s] %3d %13v %15s %s %s \r\n",
|
||||
log.Infof(" %s %3d %13v %15s %s %s \r\n",
|
||||
startTime.Format("2006-01-02 15:04:05.9999"),
|
||||
strings.ToUpper(logger.Level.String()),
|
||||
statusCode,
|
||||
latencyTime,
|
||||
clientIP,
|
||||
@@ -51,52 +48,58 @@ func LoggerToFile() gin.HandlerFunc {
|
||||
reqUri,
|
||||
)
|
||||
|
||||
if c.Request.Method != "GET" && c.Request.Method != "OPTIONS" {
|
||||
menu := models.Menu{}
|
||||
menu.Path = reqUri
|
||||
menu.Action = reqMethod
|
||||
menuList, _ := menu.Get()
|
||||
sysOperLog := models.SysOperLog{}
|
||||
sysOperLog.OperIp = clientIP
|
||||
sysOperLog.OperLocation = tools.GetLocation(clientIP)
|
||||
sysOperLog.Status = tools.IntToString(statusCode)
|
||||
sysOperLog.OperName = tools.GetUserName(c)
|
||||
sysOperLog.RequestMethod = c.Request.Method
|
||||
sysOperLog.OperUrl = reqUri
|
||||
if reqUri == "/login" {
|
||||
sysOperLog.BusinessType = "10"
|
||||
sysOperLog.Title = "用户登录"
|
||||
sysOperLog.OperName = "-"
|
||||
} else if strings.Contains(reqUri, "/api/v1/logout") {
|
||||
sysOperLog.BusinessType = "11"
|
||||
} else if strings.Contains(reqUri, "/api/v1/getCaptcha") {
|
||||
sysOperLog.BusinessType = "12"
|
||||
sysOperLog.Title = "验证码"
|
||||
} else {
|
||||
if reqMethod == "POST" {
|
||||
sysOperLog.BusinessType = "1"
|
||||
} else if reqMethod == "PUT" {
|
||||
sysOperLog.BusinessType = "2"
|
||||
} else if reqMethod == "DELETE" {
|
||||
sysOperLog.BusinessType = "3"
|
||||
}
|
||||
}
|
||||
sysOperLog.Method = reqMethod
|
||||
if len(menuList) > 0 {
|
||||
sysOperLog.Title = menuList[0].Title
|
||||
}
|
||||
b, _ := c.Get("body")
|
||||
sysOperLog.OperParam, _ = tools.StructToJsonStr(b)
|
||||
sysOperLog.CreateBy = tools.GetUserName(c)
|
||||
sysOperLog.OperTime = tools.GetCurrntTime()
|
||||
sysOperLog.LatencyTime = (latencyTime).String()
|
||||
sysOperLog.UserAgent = c.Request.UserAgent()
|
||||
if c.Err() == nil {
|
||||
sysOperLog.Status = "0"
|
||||
} else {
|
||||
sysOperLog.Status = "1"
|
||||
}
|
||||
_, _ = sysOperLog.Create()
|
||||
if c.Request.Method != "GET" && c.Request.Method != "OPTIONS" && config2.LogConfig.Operdb {
|
||||
SetDBOperLog(c, clientIP, statusCode, reqUri, reqMethod, latencyTime)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 写入操作日志表
|
||||
// 该方法后续即将弃用
|
||||
func SetDBOperLog(c *gin.Context, clientIP string, statusCode int, reqUri string, reqMethod string, latencyTime time.Duration) {
|
||||
menu := models.Menu{}
|
||||
menu.Path = reqUri
|
||||
menu.Action = reqMethod
|
||||
menuList, _ := menu.Get()
|
||||
sysOperLog := models.SysOperLog{}
|
||||
sysOperLog.OperIp = clientIP
|
||||
sysOperLog.OperLocation = tools.GetLocation(clientIP)
|
||||
sysOperLog.Status = tools.IntToString(statusCode)
|
||||
sysOperLog.OperName = tools.GetUserName(c)
|
||||
sysOperLog.RequestMethod = c.Request.Method
|
||||
sysOperLog.OperUrl = reqUri
|
||||
if reqUri == "/login" {
|
||||
sysOperLog.BusinessType = "10"
|
||||
sysOperLog.Title = "用户登录"
|
||||
sysOperLog.OperName = "-"
|
||||
} else if strings.Contains(reqUri, "/api/v1/logout") {
|
||||
sysOperLog.BusinessType = "11"
|
||||
} else if strings.Contains(reqUri, "/api/v1/getCaptcha") {
|
||||
sysOperLog.BusinessType = "12"
|
||||
sysOperLog.Title = "验证码"
|
||||
} else {
|
||||
if reqMethod == "POST" {
|
||||
sysOperLog.BusinessType = "1"
|
||||
} else if reqMethod == "PUT" {
|
||||
sysOperLog.BusinessType = "2"
|
||||
} else if reqMethod == "DELETE" {
|
||||
sysOperLog.BusinessType = "3"
|
||||
}
|
||||
}
|
||||
sysOperLog.Method = reqMethod
|
||||
if len(menuList) > 0 {
|
||||
sysOperLog.Title = menuList[0].Title
|
||||
}
|
||||
b, _ := c.Get("body")
|
||||
sysOperLog.OperParam, _ = tools.StructToJsonStr(b)
|
||||
sysOperLog.CreateBy = tools.GetUserName(c)
|
||||
sysOperLog.OperTime = tools.GetCurrntTime()
|
||||
sysOperLog.LatencyTime = (latencyTime).String()
|
||||
sysOperLog.UserAgent = c.Request.UserAgent()
|
||||
if c.Err() == nil {
|
||||
sysOperLog.Status = "0"
|
||||
} else {
|
||||
sysOperLog.Status = "1"
|
||||
}
|
||||
_, _ = sysOperLog.Create()
|
||||
}
|
||||
Reference in new issue
Block a user