main.go 1.3 KB

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