panic_advice.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package exception
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/core/logs"
  5. "github.com/beego/beego/v2/server/web"
  6. "github.com/beego/beego/v2/server/web/context"
  7. "runtime"
  8. )
  9. func PanicAdvice(ctx *context.Context, conf *web.Config) {
  10. if err := recover(); err != nil {
  11. if err == web.ErrAbort {
  12. return
  13. }
  14. if !web.BConfig.RecoverPanic {
  15. panic(err)
  16. }
  17. stack := ""
  18. msg := fmt.Sprintf("The request url is %v", ctx.Input.URL())
  19. stack += msg + "</br>"
  20. logs.Critical(msg)
  21. msg = fmt.Sprintf("The request data is %v", string(ctx.Input.RequestBody))
  22. stack += msg + "</br>"
  23. logs.Critical(msg)
  24. msg = fmt.Sprintf("Handler crashed with error %v", err)
  25. stack += msg + "</br>"
  26. logs.Critical(msg)
  27. for i := 1; ; i++ {
  28. _, file, line, ok := runtime.Caller(i)
  29. if !ok {
  30. break
  31. }
  32. logs.Critical(fmt.Sprintf("%s:%d", file, line))
  33. stack = stack + fmt.Sprintln(fmt.Sprintf("%s:%d</br>", file, line))
  34. }
  35. //go utils.SendEmail(utils.APPNAME+"崩了"+time.Now().Format("2006-01-02 15:04:05"), stack, utils.EmailSendToUsers)
  36. //go alarm_msg.SendAlarmMsg(utils.APPNAME+"崩了"+time.Now().Format("2006-01-02 15:04:05")+"<br/>"+stack, 3)
  37. }
  38. return
  39. }