serverLogUtil.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. -- 服务端埋点日志工具文件
  2. local skynet = require "skynet"
  3. local timeUtil = require "utils.timeUtil"
  4. local lib_game_redis = require("lib_game_redis")
  5. local redisUtil = require("utils.redisUtil")
  6. local root = {}
  7. local function writeServerLog(key, date, data)
  8. assert(key and date)
  9. skynet.send("statisticLog", "lua", "add_file_log", key, date, data)
  10. end
  11. function root.getActiveKey(date)
  12. return "active:uid:" .. date
  13. end
  14. function root.formatData(data)
  15. return table.concat(data, ";")
  16. end
  17. function root.formatContent(data)
  18. return table.concat(data, ",")
  19. end
  20. -- 新增活跃天玩家
  21. local function l_add_active_user(uid)
  22. local currTime = timeUtil.currentTime()
  23. local date = timeUtil.toDate(currTime)
  24. local key = root.getActiveKey(date)
  25. -- log.info("l_add_active_user key[%s] uid[%s]", tostring(key), tostring(uid))
  26. lib_game_redis:sadd(key, uid)
  27. end
  28. -- 服务端事件埋点
  29. function root.logEvent(uid, bandShareCode, version, channel, eventId, extraParams)
  30. local now = timeUtil.currentTime()
  31. local date = timeUtil.toDate(now)
  32. local dateStr = timeUtil.toString(now)
  33. local cnt =
  34. string.format(
  35. "%s;%s;%s;%s;%s;%s;%s;%s",
  36. tostring(date),
  37. tostring(dateStr),
  38. tostring(uid or ""),
  39. tostring(bandShareCode or ""),
  40. tostring(channel or 0),
  41. tostring(version or ""),
  42. tostring(eventId or ""),
  43. tostring(extraParams or "")
  44. )
  45. writeServerLog("event", date, cnt)
  46. end
  47. -- 服务端资源埋点
  48. function root.logResource(uid, eventId, itemId, resChgs, resResult, channel, bandShareCode)
  49. local now = timeUtil.currentTime()
  50. local date = timeUtil.toDate(now)
  51. local dateStr = timeUtil.toString(now)
  52. local cnt =
  53. string.format(
  54. "%s;%s;%s;%s;%s;%s;%s;%s;%s",
  55. tostring(date),
  56. tostring(dateStr),
  57. tostring(uid),
  58. tostring(bandShareCode or ""),
  59. tostring(channel),
  60. tostring(eventId),
  61. tostring(itemId),
  62. tostring(resChgs or 0),
  63. tostring(resResult or 0)
  64. )
  65. writeServerLog("resource", date, cnt)
  66. end
  67. -- 服务端注册埋点
  68. function root.logRegister(uid, bandShareCode, channel, version, machineType, uuid, udid, ip)
  69. local now = timeUtil.currentTime()
  70. local date = timeUtil.toDate(now)
  71. local dateStr = timeUtil.toString(now)
  72. local data = {
  73. date,
  74. dateStr,
  75. uid or "",
  76. bandShareCode or "",
  77. channel or "",
  78. version or "",
  79. machineType or "",
  80. uuid or "",
  81. udid or "",
  82. ip or ""
  83. }
  84. local dataStr = root.formatData(data)
  85. -- 添加到每日活跃信息中
  86. l_add_active_user(uid)
  87. writeServerLog("register", date, dataStr)
  88. end
  89. -- 服务端登录埋点
  90. function root.logLogin(uid, bandShareCode, channel, version, machineType, operator, network, uuid, udid, ip)
  91. local now = timeUtil.currentTime()
  92. local date = timeUtil.toDate(now)
  93. local dateStr = timeUtil.toString(now)
  94. local data = {
  95. date,
  96. dateStr,
  97. uid or 0,
  98. bandShareCode or "",
  99. channel or "",
  100. version or "",
  101. machineType or "",
  102. operator or "",
  103. network or "",
  104. uuid or "",
  105. udid or "",
  106. ip or ""
  107. }
  108. local dataStr = root.formatData(data)
  109. -- 添加到每日活跃信息中
  110. l_add_active_user(uid)
  111. writeServerLog("login", date, dataStr)
  112. end
  113. -- 服务端登出埋点
  114. function root.logLogout(uid, bandShareCode, channel, version, runTimes)
  115. local now = timeUtil.currentTime()
  116. local date = timeUtil.toDate(now)
  117. local dateStr = timeUtil.toString(now)
  118. local data = {
  119. date,
  120. dateStr,
  121. uid or "",
  122. bandShareCode or "",
  123. channel or "",
  124. version or "",
  125. runTimes or ""
  126. }
  127. local dataStr = root.formatData(data)
  128. -- 添加到每日活跃信息中
  129. l_add_active_user(uid)
  130. writeServerLog("logout", date, dataStr)
  131. end
  132. -- 服务端支付埋点
  133. function root.logPay(playerInfo, orderInfo)
  134. if is_empty(playerInfo) or is_empty(orderInfo) then
  135. return
  136. end
  137. local now = timeUtil.currentTime()
  138. local date = timeUtil.toDate(now)
  139. local dateStr = timeUtil.toString(now)
  140. local data = {
  141. date,
  142. dateStr,
  143. playerInfo.uid or "",
  144. playerInfo.bandShareCode or "",
  145. playerInfo.channel or "",
  146. playerInfo.version or "",
  147. orderInfo.orderNo or "",
  148. orderInfo.pay or "",
  149. orderInfo.itemId or "",
  150. orderInfo.amount or "",
  151. orderInfo.status or "",
  152. orderInfo.retmsg or ""
  153. }
  154. local dataStr = root.formatData(data)
  155. writeServerLog("pay", date, dataStr)
  156. end
  157. -- 绑定推广码
  158. function root.logBandShareCode(uid, bandShareCode, channel, version)
  159. local now = timeUtil.currentTime()
  160. local date = timeUtil.toDate(now)
  161. local dateStr = timeUtil.toString(now)
  162. local data = {
  163. date,
  164. dateStr,
  165. uid or "",
  166. bandShareCode or "",
  167. channel or "",
  168. version or ""
  169. }
  170. local dataStr = root.formatData(data)
  171. writeServerLog("banding", date, dataStr)
  172. end
  173. -- 在线人数
  174. function root.logOnlineCount()
  175. local mapCount = redisUtil.hgetall("online:count")
  176. log.info("logOnlineCount mapCount[%s]", tostring(mapCount))
  177. if is_empty(mapCount) then
  178. return
  179. end
  180. local now = timeUtil.currentTime()
  181. local date = timeUtil.toDate(now)
  182. local dateStr = timeUtil.toString(now)
  183. -- 全服
  184. if mapCount["all"] then
  185. local data = {
  186. date,
  187. dateStr,
  188. "",
  189. "",
  190. mapCount["all"]
  191. }
  192. local dataStr = root.formatData(data)
  193. writeServerLog("onlinecount", date, dataStr)
  194. end
  195. -- 主播
  196. for k, v in pairs(mapCount) do
  197. if string.match(k, "bandShareCode") and tonumber(v) > 0 then
  198. local arr = string.split(k, ":")
  199. local data = {
  200. date,
  201. dateStr,
  202. arr[2],
  203. "",
  204. v
  205. }
  206. local dataStr = root.formatData(data)
  207. writeServerLog("onlinecount", date, dataStr)
  208. end
  209. end
  210. end
  211. return root