Blame view

src/services/logd.lua 2.69 KB
314bc5df   zhengshouren   提交服务器初始代码
1
  local skynet = require "skynet"
314bc5df   zhengshouren   提交服务器初始代码
2
3
  local queue = require "skynet.queue"
  local bson = require "bson"
df883a11   zhouhaihai   日志处理
4
  local socketdriver = require "skynet.socketdriver"
314bc5df   zhengshouren   提交服务器初始代码
5
6
7
8
9
10
11
12
13
14
15
  
  local serverId = tonumber(skynet.getenv("servId"))
  
  require "shared.init"
  require "skynet.manager"
  
  local table_insert = table.insert
  local pairs = pairs
  local ipairs = ipairs
  local string_format = string.format
  
df883a11   zhouhaihai   日志处理
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
  local CMD, cs = {}
  local log_fd, connecting = nil , false
  
  
  local socket_message = {}
  -- read skynet_socket.h for these macro
  -- SKYNET_SOCKET_TYPE_DATA = 1
  socket_message[1] = function(id, size, data)
  	skynet.error(string.format("LOG SOCKET: data: ", skynet.tostring(data, size)))
  	socketdriver.drop(data, size)
  end
  
  -- SKYNET_SOCKET_TYPE_CONNECT = 2
  socket_message[2] = function(id, _ , addr)
  	skynet.error("LOG SOCKET: connect: ", addr)
  	connecting = false
  end
  
  -- SKYNET_SOCKET_TYPE_CLOSE = 3
  socket_message[3] = function(id)
  	skynet.error("LOG SOCKET: closed")
  	connecting = false
  end
  
  -- SKYNET_SOCKET_TYPE_ERROR = 5
  socket_message[5] = function(id, _, err)
  	skynet.error("LOG SOCKET: error: ", err)
  	connecting = false
  end
314bc5df   zhengshouren   提交服务器初始代码
45
  
df883a11   zhouhaihai   日志处理
46
47
48
49
50
51
52
53
  
  skynet.register_protocol {
  	name = "socket",
  	id = skynet.PTYPE_SOCKET,	-- PTYPE_SOCKET = 6
  	unpack = socketdriver.unpack,
  	dispatch = function (_, _, t, ...)
  		if socket_message[t] then
  			socket_message[t](...)
314bc5df   zhengshouren   提交服务器初始代码
54
55
  		end
  	end
df883a11   zhouhaihai   日志处理
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  }
  
  
  
  -- 日志 index 不包含 日期的 index_suffix
  local IndexNoDate = {
  	online = true,
  }
  -- 不走 role log 的日志都要自行注意 mapping 设置【重要】
  -- index_suffix index 后缀 默认为 common
  function CMD.log(logType, doc, index_suffix)
  	index_suffix = index_suffix or "common"
  	if index_suffix == "common" then
  		doc["@type"] = logType
  	else
  		if logType ~= index_suffix then -- 定制后缀 不一定有type 不相等时才有type
  			doc["@type"] = logType
314bc5df   zhengshouren   提交服务器初始代码
73
74
  		end
  	end
df883a11   zhouhaihai   日志处理
75
76
77
  
  	local now = skynet.timex()
  	doc["timestamp"] = now
e3c5cc5e   zhouhaihai   跨服竞技场over
78
  	doc["timestamp_f"] = os.date("%Y-%m-%d %H:%M:%S", now)
df883a11   zhouhaihai   日志处理
79
80
81
82
83
84
85
86
87
88
89
  	doc["server"] = serverId
  
  	-- 自己加好 index
  	if IndexNoDate[index_suffix] then
  		doc["@index"] = string.format("gamelog-%s", index_suffix)
  	else
  		doc["@index"] = string.format("gamelog-%s-%s", os.date("%Y%m%d", now), index_suffix)
  	end
  	if not socketdriver.send(log_fd, json.encode(doc) .. "\n") then
  		if not connecting then
  			CMD.open() -- 连一下试试
314bc5df   zhengshouren   提交服务器初始代码
90
91
  		end
  	end
314bc5df   zhengshouren   提交服务器初始代码
92
93
  end
  
df883a11   zhouhaihai   日志处理
94
95
96
  function CMD.open()
  	log_fd = socketdriver.connect("127.0.0.1", 5170)
  	connecting = true
314bc5df   zhengshouren   提交服务器初始代码
97
98
  end
  
314bc5df   zhengshouren   提交服务器初始代码
99
  local function __init__()
df883a11   zhouhaihai   日志处理
100
  	skynet.dispatch("lua", function (session, address, command, ...)
314bc5df   zhengshouren   提交服务器初始代码
101
102
103
104
  		local f = CMD[command]
  		if command == "open" then
  			skynet.ret(skynet.pack(f(...)))
  		else
df883a11   zhouhaihai   日志处理
105
106
  			local logType, doc, index_suffix = ...
  			cs(function() f(logType, doc, index_suffix) end)
314bc5df   zhengshouren   提交服务器初始代码
107
108
109
110
  		end
  	end)
  	cs = queue()
  
df883a11   zhouhaihai   日志处理
111
  	skynet.register(".LOGD")
314bc5df   zhengshouren   提交服务器初始代码
112
113
114
  end
  
  skynet.start(__init__)