增加定时任务 处理异常
This commit is contained in:
57
middleware/log.go
Normal file
57
middleware/log.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"energy-management-system/global"
|
||||
"energy-management-system/utils/exception"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Log(c *gin.Context) {
|
||||
// 开始时间
|
||||
startTime := time.Now()
|
||||
var body []byte
|
||||
if c.Request.Method != "GET" {
|
||||
if strings.Contains(c.GetHeader("Content-Type"), "application/json") {
|
||||
var e error
|
||||
if body, e = io.ReadAll(c.Request.Body); e == nil {
|
||||
c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
|
||||
}
|
||||
}
|
||||
} // 处理请求
|
||||
c.Next()
|
||||
// 执行时间
|
||||
latencyTime := time.Now().Sub(startTime).String()
|
||||
// 日志格式
|
||||
path, _ := url.PathUnescape(c.Request.RequestURI)
|
||||
//
|
||||
field := map[string]any{
|
||||
"ip": c.ClientIP(),
|
||||
"method": c.Request.Method,
|
||||
"url": path,
|
||||
"code": c.Writer.Status(),
|
||||
"period": latencyTime,
|
||||
"model": "gin",
|
||||
}
|
||||
if len(global.Log.Heads) > 0 {
|
||||
for _, v := range global.Log.Heads {
|
||||
field[v] = c.Request.Header.Get(v)
|
||||
}
|
||||
}
|
||||
if err := c.Errors.Last(); err != nil {
|
||||
if h, ok := err.Meta.(*exception.E); ok {
|
||||
field["code"] = h.Code
|
||||
field["errMsg"] = h.Msg
|
||||
if h.Err != nil {
|
||||
field["err"] = h.Err.Error()
|
||||
}
|
||||
}
|
||||
global.Log.WithFields(field).Error(string(body))
|
||||
} else {
|
||||
global.Log.WithFields(field).Info(string(body))
|
||||
}
|
||||
}
|
||||
@@ -1,48 +1,2 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"energy-management-system/utils"
|
||||
"energy-management-system/utils/exception"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Recovery() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
var brokenPipe bool
|
||||
if ne, ok := err.(*net.OpError); ok {
|
||||
if se, ok := ne.Err.(*os.SyscallError); ok {
|
||||
if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
|
||||
brokenPipe = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if brokenPipe {
|
||||
c.Error(err.(error))
|
||||
} else {
|
||||
if h, ok := err.(*exception.Exception); ok {
|
||||
exception.Panic(c, h)
|
||||
c.Errors = append(c.Errors, &gin.Error{Meta: h})
|
||||
} else if _, ok = err.(error); ok {
|
||||
if gin.IsDebugging() {
|
||||
fmt.Printf("[Recovery] %s : %s", utils.TimeFormat(time.Now()), err)
|
||||
utils.Stack(3)
|
||||
}
|
||||
exception.Unknow(c)
|
||||
} else {
|
||||
fmt.Print(err)
|
||||
exception.Server(c)
|
||||
}
|
||||
}
|
||||
c.Abort()
|
||||
}
|
||||
}()
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user