package middleware import ( "bytes" "github.com/gin-gonic/gin" "go.uber.org/zap" "io/ioutil" "net" "net/http" "net/http/httputil" "os" "runtime/debug" "strings" "time" ) func NewLogger() gin.HandlerFunc { return func(c *gin.Context) { // 开始时间 startTime := time.Now() // 处理请求 endTime := time.Now() // 执行时间 latencyTime := endTime.Sub(startTime) // 请求方式 path := c.Request.URL.Path query := c.Request.URL.RawQuery data, _ := ioutil.ReadAll(c.Request.Body) dataStr := string(data) if len(dataStr) > 1000 { // 数据太大就不记录了 dataStr = "" } zap.L().Info("path", zap.Int("status", c.Writer.Status()), zap.String("method", c.Request.Method), zap.String("path", path), zap.String("query", query), zap.Any("body", dataStr), zap.String("ip", c.ClientIP()), zap.String("user-agent", c.Request.UserAgent()), zap.String("errors", c.Errors.ByType(gin.ErrorTypePrivate).String()), zap.Duration("latencyTime", latencyTime)) c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(data)) c.Next() } } // GinRecovery recover掉项目可能出现的panic,并记录日志 func GinRecovery(stack bool) gin.HandlerFunc { return func(c *gin.Context) { defer func() { if err := recover(); err != nil { // Check for a broken connection, as it is not really a // condition that warrants a panic stack trace. 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) if brokenPipe { zap.L().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 } if stack { zap.L().Error("[Recovery from panic]", zap.Any("error", err), zap.String("request", string(httpRequest)), zap.String("stack", string(debug.Stack())), ) } else { zap.L().Error("[Recovery from panic]", zap.Any("error", err), zap.String("request", string(httpRequest)), ) } c.AbortWithStatus(http.StatusInternalServerError) } }() c.Next() } }