Skip to content

Commit

Permalink
Update 异常收集
Browse files Browse the repository at this point in the history
  • Loading branch information
xinliangnote committed Sep 4, 2019
1 parent 54c7c2a commit 111cf2d
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions app/route/middleware/exception/exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,31 @@ import (
"go-gin-api/app/util"
"runtime/debug"
"strings"
"time"
)

func SetUp() gin.HandlerFunc {

return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
subject := fmt.Sprintf("[Panic - %s] 项目出错了!", config.AppName)
body := fmt.Sprintf("<b>错误时间:%s\n Runtime:\n</b>%s",time.Now().Format("2006/01/02 - 15:04:05"), string(debug.Stack()))
subject := fmt.Sprintf("【重要错误】%s 项目出错了!", config.AppName)

body := fmt.Sprintf("<b>ErrorMessage: </b> %s \n", err)
body += fmt.Sprintf("<b>RequestTime: </b> %s \n", util.GetCurrentDate())
body += fmt.Sprintf("<b>RequestURL: </b> %s %s \n", c.Request.Method, c.Request.RequestURI)
body += fmt.Sprintf("<b>RequestProto: </b> %s \n", c.Request.Proto)
body += fmt.Sprintf("<b>RequestReferer: </b> %s \n", c.Request.Referer())
body += fmt.Sprintf("<b>RequestUA: </b> %s \n", c.Request.UserAgent())
body += fmt.Sprintf("<b>RequestClientIp: </b> %s \n", c.ClientIP())
body += fmt.Sprintf("<b>DebugStack: </b> %s \n", string(debug.Stack()))

bodyHtml := ""
for _,v := range strings.Split(body, "\n") {
for _, v := range strings.Split(body, "\n") {
bodyHtml += v + "<br>"
}
_ = util.SendMail(config.ErrorNotifyUser, subject, bodyHtml)

utilGin := util.Gin{Ctx:c}
utilGin := util.Gin{Ctx: c}
utilGin.Response(500, "系统异常,请联系管理员!", nil)
}
}()
Expand Down

0 comments on commit 111cf2d

Please sign in to comment.