2 Star 0 Fork 0

wuzheng0709 / backend-gopkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ginZap.go 3.37 KB
一键复制 编辑 原始数据 按行查看 历史
HCY 提交于 2024-05-10 13:07 . edit pkg
package log
import (
"gitee.com/wuzheng0709/backend-gopkg/infrastructure/pkg/code"
"github.com/getsentry/sentry-go"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"net"
"net/http/httputil"
"os"
"runtime/debug"
"strings"
"time"
)
func GinLogger() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
query := c.Request.URL.RawQuery
c.Next()
logger := TeamsLog
latency := time.Since(start)
logger.Info(path,
zap.Int("status", c.Writer.Status()),
zap.String("method", c.Request.Method),
zap.String("path", path),
zap.String("query", query),
zap.String("ip", c.Request.Header.Get("X-Real-IP")),
zap.String("user-agent", c.Request.UserAgent()),
zap.String("errors", c.Errors.ByType(gin.ErrorTypePrivate).String()),
zap.Duration("latency", latency),
)
}
}
func Logger(logger *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
query := c.Request.URL.RawQuery
c.Next()
if logger == nil {
logger = zap.L()
}
latency := time.Since(start)
logger.Info(path,
zap.Int("status", c.Writer.Status()),
zap.String("method", c.Request.Method),
zap.String("path", path),
zap.String("query", query),
zap.String("ip", c.Request.Header.Get("X-Real-IP")),
zap.String("user-agent", c.Request.UserAgent()),
zap.String("errors", c.Errors.ByType(gin.ErrorTypePrivate).String()),
zap.Duration("latency", latency),
)
if c.Writer.Status() >= 400 {
sentry.CaptureMessage("请求失败" + path + string(c.Writer.Status()) + query + c.Errors.ByType(gin.ErrorTypePrivate).String() + string(latency))
}
if latency >= 2000 {
sentry.CaptureMessage("慢请求,待优化" + path + string(c.Writer.Status()) + query + c.Errors.ByType(gin.ErrorTypePrivate).String() + string(latency))
}
}
}
func Recovery(logger *zap.Logger, stack bool) gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
if logger == nil {
logger = zap.L()
}
Info("============")
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
}
}
}
httpRequest, _ := httputil.DumpRequest(c.Request, false)
Info("============2")
if brokenPipe {
logger.Error(c.Request.URL.Path,
zap.Any("error", err),
zap.String("request", string(httpRequest)),
)
// If the connection is dead, we can't write a status to it.
c.Error(err.(error)) // nolint: errcheck
c.Abort()
return
}
sentry.CaptureMessage(err.(error).Error() + string(httpRequest) + string(debug.Stack()))
if stack {
logger.Error("[Recovery from panic]",
zap.Any("error", err),
zap.String("request", string(httpRequest)),
zap.String("stack", string(debug.Stack())),
)
} else {
logger.Error("[Recovery from panic]",
zap.Any("error", err),
zap.String("request", string(httpRequest)),
)
}
Error(err.(error).Error())
Error(string(httpRequest))
Error(string(debug.Stack()))
c.JSON(500, gin.H{"code": code.Crash_Error, "msg": "发生错误,已自动上报"})
c.Abort()
return
}
}()
c.Next()
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wuzheng0709/backend-gopkg.git
git@gitee.com:wuzheng0709/backend-gopkg.git
wuzheng0709
backend-gopkg
backend-gopkg
v1.3.6

搜索帮助

344bd9b3 5694891 D2dac590 5694891