logger.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package middleware
  2. import (
  3. "bufio"
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "net/http"
  10. "strings"
  11. "time"
  12. "git.cestong.com.cn/cecf/config-center-server/common"
  13. gaConfig "git.cestong.com.cn/cecf/config-center-server/config"
  14. "github.com/gin-gonic/gin"
  15. "github.com/go-admin-team/go-admin-core/sdk"
  16. "github.com/go-admin-team/go-admin-core/sdk/api"
  17. "github.com/go-admin-team/go-admin-core/sdk/config"
  18. "github.com/go-admin-team/go-admin-core/sdk/pkg"
  19. "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
  20. "git.cestong.com.cn/cecf/config-center-server/common/global"
  21. )
  22. // LoggerToFile 日志记录到文件
  23. func LoggerToFile() gin.HandlerFunc {
  24. return func(c *gin.Context) {
  25. log := api.GetRequestLogger(c)
  26. // 开始时间
  27. startTime := time.Now()
  28. // 处理请求
  29. var body string
  30. switch c.Request.Method {
  31. case http.MethodPost, http.MethodPut, http.MethodGet, http.MethodDelete:
  32. bf := bytes.NewBuffer(nil)
  33. wt := bufio.NewWriter(bf)
  34. _, err := io.Copy(wt, c.Request.Body)
  35. if err != nil {
  36. log.Warnf("copy body error, %s", err.Error())
  37. err = nil
  38. }
  39. rb, _ := ioutil.ReadAll(bf)
  40. c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(rb))
  41. body = string(rb)
  42. }
  43. c.Next()
  44. url := c.Request.RequestURI
  45. if strings.Index(url, "logout") > -1 ||
  46. strings.Index(url, "login") > -1 {
  47. return
  48. }
  49. // 结束时间
  50. endTime := time.Now()
  51. if c.Request.Method == http.MethodOptions {
  52. return
  53. }
  54. rt, bl := c.Get("result")
  55. var result = ""
  56. if bl {
  57. rb, err := json.Marshal(rt)
  58. if err != nil {
  59. log.Warnf("json Marshal result error, %s", err.Error())
  60. } else {
  61. result = string(rb)
  62. }
  63. }
  64. st, bl := c.Get("status")
  65. var statusBus = 0
  66. if bl {
  67. statusBus = st.(int)
  68. }
  69. // 请求方式
  70. reqMethod := c.Request.Method
  71. // 请求路由
  72. reqUri := c.Request.RequestURI
  73. // 状态码
  74. statusCode := c.Writer.Status()
  75. // 请求IP
  76. clientIP := common.GetClientIP(c)
  77. // 执行时间
  78. latencyTime := endTime.Sub(startTime)
  79. // 日志格式
  80. logData := map[string]interface{}{
  81. "statusCode": statusCode,
  82. "latencyTime": latencyTime,
  83. "clientIP": clientIP,
  84. "method": reqMethod,
  85. "uri": reqUri,
  86. }
  87. log.WithFields(logData).Info()
  88. if c.Request.Method != "OPTIONS" && config.LoggerConfig.EnabledDB && statusCode != 404 {
  89. SetDBOperLog(c, clientIP, statusCode, reqUri, reqMethod, latencyTime, body, result, statusBus)
  90. }
  91. }
  92. }
  93. // SetDBOperLog 写入操作日志表 fixme 该方法后续即将弃用
  94. func SetDBOperLog(c *gin.Context, clientIP string, statusCode int, reqUri string, reqMethod string, latencyTime time.Duration, body string, result string, status int) {
  95. log := api.GetRequestLogger(c)
  96. l := make(map[string]interface{})
  97. l["_fullPath"] = c.FullPath()
  98. l["operUrl"] = reqUri
  99. l["operIp"] = clientIP
  100. fmt.Println("gaConfig.ExtConfig.AMap.Key", gaConfig.ExtConfig.AMap.Key)
  101. l["operLocation"] = pkg.GetLocation(clientIP, gaConfig.ExtConfig.AMap.Key)
  102. l["operName"] = user.GetUserName(c)
  103. l["requestMethod"] = c.Request.Method
  104. l["operParam"] = body
  105. l["operTime"] = time.Now()
  106. l["jsonResult"] = result
  107. l["latencyTime"] = latencyTime.String()
  108. l["statusCode"] = statusCode
  109. if status == http.StatusOK {
  110. l["status"] = "2"
  111. } else {
  112. l["status"] = "1"
  113. }
  114. q := sdk.Runtime.GetMemoryQueue(c.Request.Host)
  115. message, err := sdk.Runtime.GetStreamMessage("", global.OperateLog, l)
  116. if err != nil {
  117. log.Errorf("GetStreamMessage error, %s", err.Error())
  118. //日志报错错误,不中断请求
  119. } else {
  120. err = q.Append(message)
  121. if err != nil {
  122. log.Errorf("Append message error, %s", err.Error())
  123. }
  124. }
  125. }