Blame view

models/RoleLog.go 1.41 KB
a2d4f770   zhangqijia   fix: 首次通关记录到数据库
1
  package models
fc3e08ac   zhangqijia   feat: add log com...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  
  import (
  	"encoding/json"
  	"fmt"
  	"pro2d/common"
  	"pro2d/common/logger"
  	"sync/atomic"
  )
  
  var LogType = map[string]string{
  	"in_item":   "common",
  	"out_item":  "common",
  	"in_hero":   "common",
  	"out_hero":  "common",
  	"in_equip":  "common",
  	"out_equip": "common",
  
  	"role_action": "common",
  }
  
  var commonRoleField = []string{
  	"id",
  	"uid",
  	"name",
  	"device",
  	"level",
  }
  
  func (m *RoleModel) MyLog(logType string, content map[string]interface{}) {
  	_, ok := LogType[logType]
  	if !ok {
  		logger.Error("LOG ERROR: new logType [%s] need Add Maping.", logType)
  		return
  	}
  
  	doc := make(map[string]interface{})
  	for _, field := range commonRoleField {
  		if content[field] != nil {
  			logger.Error("LOG ERROR: logType [%s] had field [%s] overwrite default.", logType, field)
  		}
  
  		v := m.GetProperty(field)
  		if v != nil {
  			doc[field] = v
  		}
  	}
  	content["ucode"] = m.GetActionUnicode()
  	for field, value := range content {
  		doc[field] = value
  	}
  	doc["@type"] = logType
  	j, err := json.Marshal(doc)
  	if err != nil {
  		logger.Error("LOG ERROR: logType [%s]  json.Marshal", logType)
  		return
  	}
  	logger.Debug(j)
  }
  
  func (m *RoleModel) StartActionUnicode() {
  	uniqueCount := atomic.AddInt64(&m.uniqueCount, 1)
  	m.actionUcode = fmt.Sprintf("%s-%d-%d", m.Data.Id, common.Timex(), uniqueCount)
  }
  
  func (m *RoleModel) EndActionUnicode() {
  	m.actionUcode = ""
  }
  
  func (m *RoleModel) GetActionUnicode() string {
  	return m.actionUcode
  }