MiddlewareLog.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package middle
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/net/ghttp"
  5. "github.com/gogf/gf/os/glog"
  6. "github.com/gogf/gf/os/gtime"
  7. "github.com/gogf/gf/text/gstr"
  8. "github.com/gogf/gf/util/gconv"
  9. "gmanager/app/constants"
  10. "gmanager/library/base"
  11. )
  12. func MiddlewareLog(r *ghttp.Request) {
  13. var beforeTime int64
  14. var params map[string]interface{}
  15. var no string
  16. if constants.DEBUG {
  17. beforeTime = gtime.TimestampMilli()
  18. if r.IsFileRequest() {
  19. return
  20. }
  21. if r.Method == "GET" {
  22. params = r.GetMap()
  23. } else if r.Method == "POST" {
  24. params = r.GetMap()
  25. } else {
  26. base.Error(r, "Request Method is ERROR! ")
  27. return
  28. }
  29. no = gconv.String(params["no"])
  30. if no == "" {
  31. no = gconv.String(beforeTime)
  32. }
  33. glog.Info(fmt.Sprintf("[REQUEST_%s][url:%s][params:%s]",
  34. no, r.URL.Path, params))
  35. }
  36. r.Middleware.Next()
  37. // 青牛完成
  38. if constants.DEBUG {
  39. data := string(r.Response.Buffer())
  40. if r.URL.Path == "" || r.URL.Path == "/" || gstr.Contains(
  41. r.URL.Path, "index") || gstr.Contains(
  42. r.URL.Path, "html") || gstr.Contains(
  43. r.URL.Path, "login") {
  44. data = ""
  45. }
  46. afterTime := gtime.TimestampMilli()
  47. if r.IsFileRequest() {
  48. glog.Info(fmt.Sprintf("[FILE_%s][diff:%d][url:%s][params:%s]",
  49. no, afterTime-beforeTime, r.URL.Path, params))
  50. } else if afterTime-beforeTime > 1000 {
  51. glog.Warning(fmt.Sprintf("[RESPONSE_%s][diff:%d][url:%s][params:%s][data:%s]",
  52. no, afterTime-beforeTime, r.URL.Path, params, data))
  53. } else {
  54. glog.Info(fmt.Sprintf("[RESPONSE_%s][diff:%d][url:%s][params:%s][data:%s]",
  55. no, afterTime-beforeTime, r.URL.Path, params, data))
  56. }
  57. }
  58. }
  59. func MiddlewareCommon(r *ghttp.Request) {
  60. r.SetParam("BASE_PATH", "")
  61. r.Middleware.Next()
  62. }