Skip to content

Commit

Permalink
日志模块优化
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo0812 committed Sep 21, 2023
1 parent 690cf41 commit 0dfc001
Show file tree
Hide file tree
Showing 42 changed files with 210 additions and 239 deletions.
8 changes: 4 additions & 4 deletions script/agent/cache_agent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ end
function CacheAgent:load(primary_id, sheet_name)
local ok, code, row_data = router_mgr:call_cache_hash(primary_id, "rpc_cache_load", primary_id, sheet_name)
if qfailed(code, ok) then
log_err("[CacheAgent][load] code=%s, pkey=%s, sheet_name=%s", code, primary_id, sheet_name)
log_err("[CacheAgent][load] code={}, pkey={}, sheet_name={}", code, primary_id, sheet_name)
return ok and code or RPC_FAILED
end
return code, row_data
Expand All @@ -25,7 +25,7 @@ end
function CacheAgent:update_field(primary_id, sheet_name, field, field_data)
local ok, code = router_mgr:call_cache_hash(primary_id, "rpc_cache_update_field", primary_id, sheet_name, field, field_data)
if qfailed(code, ok) then
log_err("[CacheAgent][update_field] faild: code=%s, sheet_name=%s, primary_id=%s", code, sheet_name, primary_id)
log_err("[CacheAgent][update_field] faild: code={}, sheet_name={}, primary_id={}", code, sheet_name, primary_id)
return ok and code or RPC_FAILED
end
return code
Expand All @@ -35,7 +35,7 @@ end
function CacheAgent:remove_field(primary_id, sheet_name, field)
local ok, code = router_mgr:call_cache_hash(primary_id, "rpc_cache_remove_field", primary_id, sheet_name, field)
if qfailed(code, ok) then
log_err("[CacheAgent][remove_field] faild: code=%s, sheet_name=%s, primary_id=%s", code, sheet_name, primary_id)
log_err("[CacheAgent][remove_field] faild: code={}, sheet_name={}, primary_id={}", code, sheet_name, primary_id)
return ok and code or RPC_FAILED
end
return code
Expand All @@ -45,7 +45,7 @@ end
function CacheAgent:delete(primary_id, sheet_name)
local ok, code = router_mgr:call_cache_hash(primary_id, "rpc_cache_delete", primary_id, sheet_name)
if qfailed(code, ok) then
log_err("[CacheAgent][delete] faild: code=%s, sheet_name=%s, primary_id=%s", code, sheet_name, primary_id)
log_err("[CacheAgent][delete] faild: code={}, sheet_name={}, primary_id={}", code, sheet_name, primary_id)
return ok and code or RPC_FAILED
end
return code
Expand Down
4 changes: 2 additions & 2 deletions script/agent/gm_agent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ end

-- 通知执行GM指令
function GMAgent:rpc_command_execute(cmd_name, ...)
log_info("[GMAgent][rpc_command_execute]->cmd_name:%s", cmd_name)
log_info("[GMAgent][rpc_command_execute]->cmd_name:{}", cmd_name)
local ok, res = tunpack(self:notify_listener(cmd_name, ...))
return ok and SUCCESS or LOGIC_FAILED, res
end

-- GM服务已经ready
function GMAgent:on_service_ready(id, service_name)
log_info("[GMAgent][on_service_ready]->id:%s, service_name:%s", id, service_name)
log_info("[GMAgent][on_service_ready]->id:{}, service_name:{}", id, service_name)
-- 上报gm列表
thread_mgr:success_call(2000, function()
return self:report_command(id)
Expand Down
6 changes: 3 additions & 3 deletions script/agent/monitor_agent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ end
function MonitorAgent:rpc_server_shutdown(reason)
-- 关闭会话连接
event_mgr:fire_frame(function()
log_warn("[MonitorAgent][rpc_server_shutdown]->service:%s", quanta.name)
log_warn("[MonitorAgent][rpc_server_shutdown]->service:{}", quanta.name)
self.client:close()
signal_quit()
end)
Expand All @@ -90,7 +90,7 @@ function MonitorAgent:rpc_remote_message(message, data)
end
local ok, code, res = tunpack(event_mgr:notify_listener(message, data))
if qfailed(code, ok) then
log_err("[MonitorAgent][rpc_remote_message] web_rpc faild: ok=%s, ec=%s", ok, code)
log_err("[MonitorAgent][rpc_remote_message] web_rpc faild: ok={}, ec={}", ok, code)
return { code = ok and code or RPC_FAILED, msg = ok and "" or code}
end
return { code = 0, msg = res}
Expand Down Expand Up @@ -119,7 +119,7 @@ function MonitorAgent:rpc_service_hotfix()
end

function MonitorAgent:rpc_set_logger_level(level)
log_debug("[MonitorAgent][rpc_set_logger_level] level: %s", level)
log_debug("[MonitorAgent][rpc_set_logger_level] level: {}", level)
log_filter(level)
end

Expand Down
16 changes: 8 additions & 8 deletions script/basic/cmdline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ end
--command = "player_id|integer aa|table bb|string dd|number"
function Cmdline:register_command(name, command, desc, cmd_type, group, tip, example, service)
if self.commands[name] then
log_warn("[Cmdline][register_command] command (%s) repeat registered!", name)
log_warn("[Cmdline][register_command] command ({}) repeat registered!", name)
return false
end
local def_args = {}
Expand All @@ -112,7 +112,7 @@ function Cmdline:register_command(name, command, desc, cmd_type, group, tip, exa
--组织显示结构
local nodes = self:find_group(group)
tinsert(nodes, { text = desc, name = name, command = command, tip = tip, example = example, tag = "gm" })
log_info("[Cmdline][register_command] command (%s) registered!", name)
log_info("[Cmdline][register_command] command ({}) registered!", name)
return true
end

Expand All @@ -123,7 +123,7 @@ function Cmdline:parser_data(cmd_data)
local cmd_name = cmd_data.name
local cmd_define = self.commands[cmd_name]
if not cmd_define then
log_err("[Cmdline][parser_data] invalid command (%s): isn't registered!", cmd_name)
log_err("[Cmdline][parser_data] invalid command ({}): isn't registered!", cmd_name)
return nil, "invalid command: isn't registered"
end
local define_args = cmd_define.args
Expand All @@ -132,7 +132,7 @@ function Cmdline:parser_data(cmd_data)
local arg = cmd_data[def_arg.name]
if not arg then
local err = sformat("invalid command: argument %s is not exist", def_arg.name)
log_err("[Cmdline][parser_data] (%s) %s!", cmd_name, err)
log_err("[Cmdline][parser_data] ({}) {}!", cmd_name, err)
return nil, err
end
tinsert(fmtinfos, def_arg.name)
Expand All @@ -154,12 +154,12 @@ function Cmdline:parser_command(argument)
local pattern = "([%a%d%_]+)"
local cmd_name = smatch(argument, pattern)
if not cmd_name then
log_err("[Cmdline][parser_command] invalid command (%s): name parse error!", argument)
log_err("[Cmdline][parser_command] invalid command ({}): name parse error!", argument)
return nil, "invalid command: name parse error"
end
local cmd_define = self.commands[cmd_name]
if not cmd_define then
log_err("[Cmdline][parser_command] invalid command (%s): isn't registered!", argument)
log_err("[Cmdline][parser_command] invalid command ({}): isn't registered!", argument)
return nil, "invalid command: isn't registered"
end
local define_args = cmd_define.args
Expand All @@ -168,13 +168,13 @@ function Cmdline:parser_command(argument)
end
local argsfunc = sgmatch(argument .. " ", pattern .. blank)
if not argsfunc then
log_err("[Cmdline][parser_command] invalid command (%s): format error!", argument)
log_err("[Cmdline][parser_command] invalid command ({}): format error!", argument)
return nil, "invalid command: format error"
end
local args = tpack(argsfunc())
if #args ~= (#define_args + 1) then
local err = sformat("invalid command: argument need %d but get %d", #define_args, #args)
log_err("[Cmdline][parser_command] (%s): %s!", argument, err)
log_err("[Cmdline][parser_command] ({}): {}!", argument, err)
return nil, err
end
return convert_args(args, cmd_define)
Expand Down
3 changes: 1 addition & 2 deletions script/basic/library.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
--library.lua
local sformat = string.format
local qgetenv = quanta.getenv

--加载全局扩展库,使用顶级域名
Expand Down Expand Up @@ -48,7 +47,7 @@ end
--newindex
local function _glib_newindex(o, k, v)
if librarys[k] then
log.warn(sformat("[quanta][library] try modify quanta library [%s] namespace", k))
logger.warn("[quanta][library] try modify quanta library [{}] namespace", k)
return
end
rawset(o, k, v)
Expand Down
20 changes: 10 additions & 10 deletions script/basic/listener.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function Listener:add_trigger(trigger, event, handler)
local func_name = handler or event
local callback_func = trigger[func_name]
if not callback_func or type(callback_func) ~= "function" then
log_warn("[Listener][add_trigger] event(%s) handler not define", event)
log_warn("[Listener][add_trigger] event({}) handler not define", event)
return
end
local trigger_map = self._triggers[event]
Expand All @@ -39,13 +39,13 @@ end

function Listener:add_listener(listener, event, handler)
if self._listeners[event] then
log_warn("[Listener][add_listener] event(%s) repeat!", event)
log_warn("[Listener][add_listener] event({}) repeat!", event)
return
end
local func_name = handler or event
local callback_func = listener[func_name]
if not callback_func or type(callback_func) ~= "function" then
log_warn("[Listener][add_listener] event(%s) callback not define!", event)
log_warn("[Listener][add_listener] event({}) callback not define!", event)
return
end
self._listeners[event] = qtweak({ [listener] = func_name })
Expand All @@ -57,13 +57,13 @@ end

function Listener:add_cmd_listener(listener, cmd, handler)
if self._commands[cmd] then
log_warn("[Listener][add_cmd_listener] cmd(%s) repeat!", cmd)
log_warn("[Listener][add_cmd_listener] cmd({}) repeat!", cmd)
return
end
local func_name = handler
local callback_func = listener[func_name]
if not callback_func or type(callback_func) ~= "function" then
log_warn("[Listener][add_cmd_listener] cmd(%s) handler not define!", cmd)
log_warn("[Listener][add_cmd_listener] cmd({}) handler not define!", cmd)
return
end
self._commands[cmd] = qtweak({ [listener] = func_name })
Expand All @@ -79,7 +79,7 @@ function Listener:notify_trigger(event, ...)
local callback_func = trigger[func_name]
local ok, ret = xpcall(callback_func, dtraceback, trigger, ...)
if not ok then
log_fatal("[Listener][notify_trigger] xpcall [%s:%s] failed: %s!", trigger:source(), func_name, ret)
log_fatal("[Listener][notify_trigger] xpcall [{}:{}] failed: {}!", trigger:source(), func_name, ret)
end
end
end
Expand All @@ -90,14 +90,14 @@ function Listener:notify_listener(event, ...)
local callback_func = listener[func_name]
local result = tpack(xpcall(callback_func, dtraceback, listener, ...))
if not result[1] then
log_fatal("[Listener][notify_listener] xpcall [%s:%s] failed: %s", listener:source(), func_name, result[2])
log_fatal("[Listener][notify_listener] xpcall [{}:{}] failed: {}", listener:source(), func_name, result[2])
result[2] = sformat("event %s execute failed!", event)
end
return result
end
if not self._ignores[event] then
self._ignores[event] = true
log_warn("[Listener][notify_listener] event (%s-%s) handler is nil! ", event, {...})
log_warn("[Listener][notify_listener] event ({}-{}) handler is nil! ", event, {...})
end
return tpack(false, "event handler is nil")
end
Expand All @@ -109,13 +109,13 @@ function Listener:notify_command(cmd, ...)
local callback_func = listener[func_name]
local result = tpack(xpcall(callback_func, dtraceback, listener, ...))
if not result[1] then
log_fatal("[Listener][notify_command] xpcall [%s:%s] failed: %s!", listener:source(), func_name, result[2])
log_fatal("[Listener][notify_command] xpcall [{}:{}] failed: {}!", listener:source(), func_name, result[2])
result[2] = sformat("cmd %s execute failed!", cmd)
end
return result
end
if not self._ignores[cmd] then
log_warn("[Listener][notify_command] command %s handler is nil!", cmd)
log_warn("[Listener][notify_command] command {} handler is nil!", cmd)
self._ignores[cmd] = true
end
return tpack(false, "command handler is nil")
Expand Down
10 changes: 5 additions & 5 deletions script/driver/graylog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ function GrayLog:__init()
self.proto = proto
if proto == "http" then
self.addr = sformat("http://%s:%s/gelf", ip, port)
log_info("[GrayLog][setup] setup http (%s) success!", self.addr)
log_info("[GrayLog][setup] setup http ({}) success!", self.addr)
return
end
self.ip = ip
self.port = port
if proto == "tcp" then
self.tcp = Socket(self)
update_mgr:attach_second(self)
log_info("[GrayLog][setup] setup tcp (%s:%s) success!", self.ip, self.port)
log_info("[GrayLog][setup] setup tcp ({}:{}) success!", self.ip, self.port)
return
end
self.udp = luabus.udp()
log_info("[GrayLog][setup] setup udp (%s:%s) success!", self.ip, self.port)
log_info("[GrayLog][setup] setup udp ({}:{}) success!", self.ip, self.port)
end

function GrayLog:close()
Expand All @@ -56,10 +56,10 @@ end
function GrayLog:on_second()
if not self.tcp:is_alive() then
if not self.tcp:connect(self.ip, self.port) then
log_err("[GrayLog][on_second] connect (%s:%s) failed!", self.ip, self.port, self.name)
log_err("[GrayLog][on_second] connect ({}:{}) failed!", self.ip, self.port, self.name)
return
end
log_info("[GrayLog][on_second] connect (%s:%s) success!", self.ip, self.port, self.name)
log_info("[GrayLog][on_second] connect ({}:{}) success!", self.ip, self.port, self.name)
end
end

Expand Down
22 changes: 11 additions & 11 deletions script/driver/influx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function Influx:__init(ip, port, org, bucket, token)
self.query_addr = sformat("http://%s:%s/api/v2/query", ip, port)
self.bucket_addr = sformat("http://%s:%s/api/v2/buckets", ip, port)
self.common_headers = { ["Authorization"] = sformat("Token %s", token), ["Content-type"] = "application/json" }
log_info("[Influx] influx driver(%s:%s) setup success!", ip, port)
log_info("[Influx] influx driver({}:{}) setup success!", ip, port)
end

--line protocol
Expand Down Expand Up @@ -83,7 +83,7 @@ function Influx:find_bucket(bucket_name)
local querys = { name = bucket_name }
local ok, status, res = http_client:call_get(self.bucket_addr, querys, self.common_headers)
if not ok or status >= 300 then
log_err("[Influx][find_bucket] failed! status: %s, err: %s", status, ok and res or status)
log_err("[Influx][find_bucket] failed! status: {}, err: {}", status, ok and res or status)
return false, ok and res or status
end
local response = json_decode(res)
Expand All @@ -104,7 +104,7 @@ function Influx:find_org(org_name)
local querys = { org = org_name }
local ok, status, res = http_client:call_get(self.org_addr, querys, self.common_headers)
if not ok or status >= 300 then
log_err("[Influx][find_org] failed! status: %s, err: %s", status, ok and res or status)
log_err("[Influx][find_org] failed! status: {}, err: {}", status, ok and res or status)
return false, ok and res or status
end
local response = json_decode(res)
Expand All @@ -124,7 +124,7 @@ end
function Influx:create_bucket(name, expire_time, description)
local cur_org = self:find_org(self.org_name)
if not cur_org then
log_err("[Influx][create_bucket] org(%s) config error", self.org_name)
log_err("[Influx][create_bucket] org({}) config error", self.org_name)
return false, "org config error"
end
local data = {
Expand All @@ -140,7 +140,7 @@ function Influx:create_bucket(name, expire_time, description)
}
local ok, status, res = http_client:call_post(self.bucket_addr, data, self.common_headers)
if not ok or status >= 300 then
log_err("[Influx][create_bucket] failed! status: %s, err: %s", status, ok and res or status)
log_err("[Influx][create_bucket] failed! status: {}, err: {}", status, ok and res or status)
return false, ok and res or status
end
return true, json_decode(res)
Expand All @@ -151,7 +151,7 @@ function Influx:delete_bucket_by_id(bucket_id)
local url = sformat("%s/%s", self.bucket_addr, bucket_id)
local ok, status, res = http_client:call_del(url, {}, self.common_headers)
if not ok or status >= 300 then
log_err("[Influx][delete_bucket_by_id] failed! status: %s, err: %s", status, ok and res or status)
log_err("[Influx][delete_bucket_by_id] failed! status: {}, err: {}", status, ok and res or status)
return false, ok and res or status
end
return true
Expand All @@ -161,13 +161,13 @@ end
function Influx:delete_bucket(bucket_name)
local bucket = self:find_bucket(bucket_name)
if not bucket then
log_err("[Influx][delete_bucket] failed! bucket: %s not exist", bucket_name)
log_err("[Influx][delete_bucket] failed! bucket: {} not exist", bucket_name)
return false, "bucket not exist"
end
local url = sformat("%s/%s", self.bucket_addr, bucket.id)
local ok, status, res = http_client:call_del(url, {}, self.common_headers)
if not ok or status >= 300 then
log_err("[Influx][delete_bucket] failed! status: %s, err: %s", status, ok and res or status)
log_err("[Influx][delete_bucket] failed! status: {}, err: {}", status, ok and res or status)
return false, ok and res or status
end
return true
Expand All @@ -186,7 +186,7 @@ function Influx:write(measurement, tags, fields)
local querys = { org = self.org, bucket = self.bucket }
local ok, status, res = http_client:call_post(self.write_addr, line_protocol, headers, querys)
if not ok or status >= 300 then
log_err("[Influx][write] failed! status: %s, err: %s", status, ok and res or status)
log_err("[Influx][write] failed! status: {}, err: {}", status, ok and res or status)
return false, ok and res or status
end
return true
Expand All @@ -211,7 +211,7 @@ function Influx:batch(batch_datas)
local querys = { org = self.org, bucket = self.bucket }
local ok, status, res = http_client:call_post(self.write_addr, line_protocol, headers, querys)
if not ok or status >= 300 then
log_err("[Influx][batch] failed! status: %s, err: %s", status, ok and res or status)
log_err("[Influx][batch] failed! status: {}, err: {}", status, ok and res or status)
return false, ok and res or status
end
return true
Expand All @@ -227,7 +227,7 @@ function Influx:query(script)
local querys = { org = self.org }
local ok, status, res = http_client:call_post(self.query_addr, script, headers, querys)
if not ok or status >= 300 then
log_err("[Influx][query] failed! status: %s, err: %s", status, ok and res or status)
log_err("[Influx][query] failed! status: {}, err: {}", status, ok and res or status)
return false, ok and res or status
end
return true, self:parse_csv(res)
Expand Down
Loading

0 comments on commit 0dfc001

Please sign in to comment.