logger.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package middleware
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "gadmin/config"
  6. "gadmin/internal/admin/consts"
  7. "gadmin/internal/gorm/model"
  8. "gadmin/utility/token"
  9. "github.com/gin-gonic/gin"
  10. "github.com/sirupsen/logrus"
  11. "io"
  12. "net/http"
  13. "os"
  14. "time"
  15. )
  16. // Logger 访问日志
  17. func Logger(c *gin.Context) {
  18. var (
  19. start = time.Now() // 开始时间
  20. body []byte
  21. err error
  22. )
  23. if c.Request.Method != http.MethodGet {
  24. body, err = io.ReadAll(c.Request.Body)
  25. if err != nil {
  26. logrus.Warnf("read body from request error:%v", err)
  27. } else {
  28. c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
  29. }
  30. }
  31. writer := responseBodyWriter{
  32. ResponseWriter: c.Writer,
  33. body: &bytes.Buffer{},
  34. }
  35. c.Writer = writer
  36. // 处理请求
  37. c.Next()
  38. // 结束时间
  39. var (
  40. adminID int64 = 0
  41. user = token.CurrentUser(c)
  42. getMap = make(map[string]interface{})
  43. headerMap = make(map[string]interface{})
  44. end = time.Now()
  45. )
  46. if user != nil {
  47. adminID = user.ID
  48. }
  49. for k, v := range c.Request.URL.Query() {
  50. getMap[k] = v
  51. }
  52. getByte, _ := json.Marshal(getMap)
  53. for k, v := range c.Request.Header {
  54. headerMap[k] = v
  55. }
  56. headerByte, _ := json.Marshal(headerMap)
  57. //remark := ""
  58. //if c.Request.Method == http.MethodPost {
  59. // remark = config.GetMenuName(c.Request.URL.Path)
  60. //}
  61. models := &model.AdminLog{
  62. Environment: os.Getenv("GIN_MODE"),
  63. AdminID: adminID,
  64. AppID: consts.AppAdmin,
  65. UserAgent: c.Request.UserAgent(),
  66. Method: c.Request.Method,
  67. URL: "http://" + c.Request.Host + c.Request.RequestURI,
  68. Host: c.Request.Host,
  69. Path: c.Request.URL.Path,
  70. GetData: string(getByte),
  71. PostData: string(body),
  72. HeaderData: string(headerByte),
  73. AccessIP: c.ClientIP(),
  74. StatusCode: int32(writer.Status()),
  75. ErrorMessage: c.Errors.ByType(gin.ErrorTypePrivate).String(),
  76. Data: writer.body.String(),
  77. Latency: int32(end.Sub(start).Milliseconds()),
  78. //Remark: remark,
  79. Timestamp: time.Now(),
  80. }
  81. //
  82. //logrus.Warnf("models:%#v", models)
  83. // 日志文件
  84. //config.AccessLog.WithFields(utility.JSONMethod(models)).Infoln()
  85. data := logrus.Fields{
  86. "clientIp": c.ClientIP(), // 访问IP
  87. "method": c.Request.Method, // 访问方式
  88. "get": c.Request.URL.Query().Encode(),
  89. "latency": end.Sub(start), // 执行事件
  90. "code": c.Writer.Status(), // 状态码
  91. "path": c.Request.URL.Path, // 访问路由
  92. "ua": c.Request.UserAgent(),
  93. }
  94. config.AccessLog.WithFields(data).Infoln()
  95. // 队列
  96. go config.Nats.Publish(consts.NatsTopicAdminLog, models)
  97. }
  98. type responseBodyWriter struct {
  99. gin.ResponseWriter
  100. body *bytes.Buffer
  101. }
  102. func (r responseBodyWriter) Write(b []byte) (int, error) {
  103. r.body.Write(b)
  104. return r.ResponseWriter.Write(b)
  105. }