logger_cassandra.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package middleware
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "github.com/gocql/gocql"
  8. "time"
  9. )
  10. var __logInfoChan chan *logInfo
  11. // cassandra 配置
  12. var _clusterCfg *gocql.ClusterConfig = nil
  13. func init(){
  14. defer func() {
  15. if p := recover(); p != nil {
  16. fmt.Println("ecover", p)
  17. }
  18. }()
  19. __logInfoChan = make(chan *logInfo, 1000)
  20. //// 设置cassandar 配置
  21. //_clusterCfg = gocql.NewCluster("218.14.81.38")
  22. //_clusterCfg.Keyspace = "i2_log"
  23. //_clusterCfg.Consistency = gocql.Quorum
  24. ////设置连接池的数量,默认是2个(针对每一个host,都建立起NumConns个连接)
  25. //_clusterCfg.NumConns = 3
  26. //
  27. //session, _ := _clusterCfg.CreateSession()
  28. //time.Sleep(1 * time.Second) //Sleep so the fillPool can complete.
  29. //defer session.Close()
  30. //// 日志处理
  31. //logProcess()
  32. }
  33. /**
  34. * 自定义日志响应类,主要是把resp的响应内容写入到body中
  35. */
  36. type loggerRespWriter struct{
  37. gin.ResponseWriter
  38. respBody *bytes.Buffer
  39. }
  40. // 写字节数组
  41. func (w loggerRespWriter) Write(b []byte) (int, error) {
  42. w.respBody.Write(b) // 保存
  43. return w.ResponseWriter.Write(b)
  44. }
  45. // 写字符串
  46. func (w loggerRespWriter) WriteString(s string) (int, error) {
  47. w.respBody.WriteString(s) // 保存
  48. return w.ResponseWriter.WriteString(s)
  49. }
  50. /**
  51. * 日志信息
  52. */
  53. type logInfo struct {
  54. ReqClientIp string
  55. ReqTime time.Time
  56. ReqMethod string
  57. ReqUrl string
  58. ReqProto string
  59. ReqUa string
  60. ReqReferer string
  61. ReqPostData string
  62. RespTime time.Time
  63. RespBody string
  64. RespCode string // 由 resp body 解析
  65. RespMsg string // 由 resp body 解析
  66. RespData string // 由 resp body 解析
  67. CostTime int
  68. }
  69. func LoggerCassandra()gin.HandlerFunc{
  70. return func(c *gin.Context){
  71. respWriter := &loggerRespWriter{
  72. ResponseWriter: c.Writer,
  73. respBody: bytes.NewBuffer([]byte{}),
  74. }
  75. c.Writer = respWriter // 注入自定writer
  76. logInfo := &logInfo{}
  77. logInfo.ReqTime = time.Now()
  78. // 下一个请求
  79. c.Next()
  80. // 设置对象
  81. logInfo.RespBody = respWriter.respBody.String()
  82. logInfo.RespTime = time.Now()
  83. logInfo.ReqMethod = c.Request.Method
  84. logInfo.ReqUrl = c.Request.RequestURI
  85. logInfo.ReqProto = c.Request.Proto
  86. logInfo.ReqUa = c.Request.UserAgent()
  87. logInfo.ReqReferer = c.Request.Referer()
  88. logInfo.ReqPostData = c.Request.PostForm.Encode()
  89. logInfo.ReqClientIp = c.ClientIP()
  90. __logInfoChan <- logInfo
  91. }
  92. }
  93. func logProcess(){
  94. go func(){
  95. for{
  96. select {
  97. case logInfo := <- __logInfoChan:
  98. insertLogInfo(logInfo)
  99. }
  100. }
  101. }()
  102. }
  103. func insertLogInfo(logInfo *logInfo){
  104. session, err := _clusterCfg.CreateSession()
  105. if err != nil{
  106. fmt.Println("创建cassandra session错误", err.Error())
  107. return
  108. }
  109. defer session.Close()
  110. var respBodyObj struct {
  111. Code string
  112. Msg string
  113. Data string
  114. }
  115. if logInfo.RespBody != ""{
  116. err = json.Unmarshal([]byte(logInfo.RespBody), &respBodyObj)
  117. if err != nil{
  118. fmt.Println("json.Unmarshal 错误", err.Error())
  119. }else {
  120. logInfo.RespCode = respBodyObj.Code
  121. logInfo.RespMsg = respBodyObj.Msg
  122. logInfo.RespData = respBodyObj.Data
  123. }
  124. }
  125. logInfo.CostTime = int(logInfo.RespTime.Sub(logInfo.ReqTime))
  126. // 保存轨迹
  127. fmt.Println("保存轨迹")
  128. cqlformat := `insert into
  129. log_info(req_date, req_time, req_method, req_url, req_proto, req_ua, req_referer, req_post_data, resp_time, resp_body, resp_code, resp_msg, resp_data, cost_time)
  130. values(?,?,?,?,?,?,?,?,?,?,?,?,?,?);`
  131. reqDate := logInfo.ReqTime.Format("20060102")
  132. q := session.Query(cqlformat, reqDate, logInfo.ReqTime.Format("2006-01-02 15:04:05"), logInfo.ReqMethod, logInfo.ReqUrl, logInfo.ReqProto, logInfo.ReqUa, logInfo.ReqReferer, logInfo.ReqPostData, logInfo.RespTime.Format("2006-01-02 15:04:05"), logInfo.RespBody, logInfo.RespCode, logInfo.RespMsg, logInfo.RespData, logInfo.CostTime)
  133. err = q.Exec() // 应该可以使用批量插入
  134. if err != nil{
  135. fmt.Println("插入日志错误:", err)
  136. return
  137. }
  138. }