Commit fc3e08ac28731c8dc48afec82bb4653e38d856e3

Authored by zhangqijia
1 parent c1d56a47

feat: add log component

cmd/gameserver/service/agent.go
@@ -131,11 +131,16 @@ func (c *Agent) OnMessage(msg components.IMessage) error { @@ -131,11 +131,16 @@ func (c *Agent) OnMessage(msg components.IMessage) error {
131 return c.Send(-101, msg.GetHeader().GetMsgID(), nil, msg.GetHeader().GetPreserve()) 131 return c.Send(-101, msg.GetHeader().GetMsgID(), nil, msg.GetHeader().GetPreserve())
132 } 132 }
133 133
  134 + c.Role.StartActionUnicode()
  135 +
134 // 调用协议号对应的逻辑函数 136 // 调用协议号对应的逻辑函数
135 f := method.(func(role *models.RoleModel, msg components.IMessage) (int32, interface{})) 137 f := method.(func(role *models.RoleModel, msg components.IMessage) (int32, interface{}))
136 errCode, protoMsg := f(c.Role, msg) 138 errCode, protoMsg := f(c.Role, msg)
137 logger.Debug("rsp errcode: %d, protocode: %d", errCode, msg.GetHeader().GetMsgID()) 139 logger.Debug("rsp errcode: %d, protocode: %d", errCode, msg.GetHeader().GetMsgID())
138 - return c.SendMsg(errCode, msg.GetHeader().GetMsgID(), protoMsg, msg.GetHeader().GetPreserve()) 140 + err := c.SendMsg(errCode, msg.GetHeader().GetMsgID(), protoMsg, msg.GetHeader().GetPreserve())
  141 +
  142 + c.Role.EndActionUnicode()
  143 + return err
139 } 144 }
140 145
141 func (c *Agent) OnTimer() { 146 func (c *Agent) OnTimer() {
common/components/icompontents.go
@@ -161,6 +161,7 @@ type ( @@ -161,6 +161,7 @@ type (
161 Update() 161 Update()
162 162
163 SetProperty(key string, val interface{}) 163 SetProperty(key string, val interface{})
  164 + GetProperty(key string) interface{}
164 SetProperties(properties map[string]interface{}) 165 SetProperties(properties map[string]interface{})
165 IncrProperty(key string, val int64) int64 166 IncrProperty(key string, val int64) int64
166 ParseFields(message protoreflect.Message, properties map[string]interface{}) []int32 167 ParseFields(message protoreflect.Message, properties map[string]interface{}) []int32
@@ -5,3 +5,5 @@ @@ -5,3 +5,5 @@
5 /usr/local/bin/docker-compose -f /Users/zhaolu/Documents/pro2d/docker-compose-service.yml up -d 5 /usr/local/bin/docker-compose -f /Users/zhaolu/Documents/pro2d/docker-compose-service.yml up -d
6 /usr/local/bin/docker ps -a 6 /usr/local/bin/docker ps -a
7 /usr/local/bin/docker rmi $(/usr/local/bin/docker images -f "dangling=true" -q) 7 /usr/local/bin/docker rmi $(/usr/local/bin/docker images -f "dangling=true" -q)
  8 +
  9 +docker rmi $(docker images -f "dangling=true" -q)
8 \ No newline at end of file 10 \ No newline at end of file
1 package models 1 package models
  2 +
  3 +import (
  4 + "encoding/json"
  5 + "fmt"
  6 + "pro2d/common"
  7 + "pro2d/common/logger"
  8 + "sync/atomic"
  9 +)
  10 +
  11 +var LogType = map[string]string{
  12 + "in_item": "common",
  13 + "out_item": "common",
  14 + "in_hero": "common",
  15 + "out_hero": "common",
  16 + "in_equip": "common",
  17 + "out_equip": "common",
  18 +
  19 + "role_action": "common",
  20 +}
  21 +
  22 +var commonRoleField = []string{
  23 + "id",
  24 + "uid",
  25 + "name",
  26 + "device",
  27 + "level",
  28 +}
  29 +
  30 +func (m *RoleModel) MyLog(logType string, content map[string]interface{}) {
  31 + _, ok := LogType[logType]
  32 + if !ok {
  33 + logger.Error("LOG ERROR: new logType [%s] need Add Maping.", logType)
  34 + return
  35 + }
  36 +
  37 + doc := make(map[string]interface{})
  38 + for _, field := range commonRoleField {
  39 + if content[field] != nil {
  40 + logger.Error("LOG ERROR: logType [%s] had field [%s] overwrite default.", logType, field)
  41 + }
  42 +
  43 + v := m.GetProperty(field)
  44 + if v != nil {
  45 + doc[field] = v
  46 + }
  47 + }
  48 + content["ucode"] = m.GetActionUnicode()
  49 + for field, value := range content {
  50 + doc[field] = value
  51 + }
  52 + doc["@type"] = logType
  53 + j, err := json.Marshal(doc)
  54 + if err != nil {
  55 + logger.Error("LOG ERROR: logType [%s] json.Marshal", logType)
  56 + return
  57 + }
  58 + logger.Debug(j)
  59 +}
  60 +
  61 +func (m *RoleModel) StartActionUnicode() {
  62 + uniqueCount := atomic.AddInt64(&m.uniqueCount, 1)
  63 + m.actionUcode = fmt.Sprintf("%s-%d-%d", m.Data.Id, common.Timex(), uniqueCount)
  64 +}
  65 +
  66 +func (m *RoleModel) EndActionUnicode() {
  67 + m.actionUcode = ""
  68 +}
  69 +
  70 +func (m *RoleModel) GetActionUnicode() string {
  71 + return m.actionUcode
  72 +}
@@ -21,6 +21,9 @@ type RoleModel struct { @@ -21,6 +21,9 @@ type RoleModel struct {
21 Items common.IMapStringNum //背包 21 Items common.IMapStringNum //背包
22 22
23 lastSaveTs int64 23 lastSaveTs int64
  24 +
  25 + uniqueCount int64
  26 + actionUcode string
24 } 27 }
25 28
26 func RoleExistByUid(uid string) *RoleModel { 29 func RoleExistByUid(uid string) *RoleModel {
@@ -171,6 +171,14 @@ func (s *Schema) SetProperty(key string, val interface{}) { @@ -171,6 +171,14 @@ func (s *Schema) SetProperty(key string, val interface{}) {
171 s.cacheFields[strings.ToLower(key)] = val 171 s.cacheFields[strings.ToLower(key)] = val
172 } 172 }
173 173
  174 +func (s *Schema) GetProperty(key string) interface{} {
  175 + idx, ok := s.reflectIndex[strings.ToLower(key)]
  176 + if !ok {
  177 + return nil
  178 + }
  179 + return s.reflectValue.Field(idx).Interface()
  180 +}
  181 +
174 //更新内存,并把字段缓存 182 //更新内存,并把字段缓存
175 func (s *Schema) SetProperties(properties map[string]interface{}) { 183 func (s *Schema) SetProperties(properties map[string]interface{}) {
176 for key, val := range properties { 184 for key, val := range properties {