elastic_log_def.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package model
  2. import (
  3. "bytes"
  4. "context"
  5. "rocommon/service"
  6. "rocommon/util"
  7. "runtime"
  8. "strconv"
  9. "time"
  10. )
  11. type ElasticLogST struct {
  12. Uid uint64 `json:"uid"` //用户唯一ID
  13. NickName string `json:"nickname"`
  14. LogTime time.Time `json:"logtime"`
  15. OpenId string `json:"openid"`
  16. LogType string `json:"logtype"` //日志类型,例如是登录日志,消费日志等
  17. LogDesc string `json:"logdesc"` //描述
  18. LogLine string `json:"logline"` //代码行信息
  19. NodeZone int `json:"nodezone"` //服务器节点信息
  20. NodeId int `json:"nodeid"`
  21. NodeType int `json:"nodetype"`
  22. Param1 int `json:"param1"`
  23. }
  24. /*
  25. LogType:
  26. RoleRegister
  27. RoleOnline
  28. RoleOnlineNum
  29. RoleOffline
  30. */
  31. //ro index 数据,后续可以添加其他索引模式的log数据
  32. func ElasticPutLogInfo(logInfo *ElasticLogST) {
  33. if service.GetElastic() != nil {
  34. //ServerNodeInfo
  35. logInfo.NodeZone = service.GetServiceConfig().Node.Zone
  36. logInfo.NodeId = service.GetServiceConfig().Node.Id
  37. logInfo.NodeType = service.GetServiceConfig().Node.Type
  38. logInfo.LogTime = util.GetCurrentTimeNow()
  39. _, file, line, ok := runtime.Caller(1)
  40. if !ok {
  41. file = "???"
  42. line = 0
  43. }
  44. strBuf := bytes.NewBufferString(file)
  45. strBuf.WriteString(":")
  46. strBuf.WriteString(strconv.Itoa(line))
  47. logInfo.LogLine = strBuf.String()
  48. ctx := context.Background()
  49. go func() {
  50. _, err := service.GetElastic().Index().Index("ro").BodyJson(logInfo).Do(ctx)
  51. if err != nil {
  52. util.InfoF("ElasticPutLogInfo uuid=%v err=%v", logInfo.Uid, err)
  53. return
  54. }
  55. //util.InfoF("ElasticPutLogInfo logtype= uuid=%v ok line=%v", logInfo.LogType, logInfo.Uid, logInfo.LogLine)
  56. }()
  57. } else {
  58. //util.InfoF("ElasticPutLogInfo uuid=%v elastic is nil", logInfo.Uid)
  59. }
  60. }