local skynet = require "skynet" local lib_logger = require("log.lib_logger") local table = table local string, tostring = string, tostring local pcall, type, next = pcall, type, next local log = {} local protoLogger = {} local mapLogger local mapLogHandle = {} -- 对应日志是否要打印:按位判断,所在位是0 不打印,1 打印 function log.isWrite(logName) -- 正式服不做日志输出 if not IS_TEST then -- if logName == "warning" then -- return true -- end return false end local name = string.upper(logName) local ctrlName = string.format("%s_LOG", name) local ok, data = pcall(skynet.getenv, ctrlName) if ok then data = tonumber(data) if data == 1 then return true elseif data == 0 then return false end end local posList = { INFO = 1, DEBUG = 2, PRINT = 3, PRINTTBL = 4, FATAL = 5, WARNING = 6, ERROR = 7, PROTO = 8, CLIENT = 9 } local pos = posList[name] if not pos then return true end ok, data = pcall(skynet.getenv, "write_log") if not ok then return true end local len = data and #data or 0 if pos > len then return true end local flag = tonumber(string.sub(data, pos, pos)) or 0 return flag ~= 0 end function log.format(...) local ok, str = pcall(string.format, ...) if not ok then return tostring(...) else return str end end function log.proto(...) if not IS_TEST then return end if not log.isWrite("proto") then return end local time = time_now_str() local dateStr = os.date("%Y-%m-%d", skynet_time()) if protoLogger[dateStr] == nil then local filename = "/proto-" .. dateStr protoLogger[dateStr] = lib_logger:new(filename) end protoLogger[dateStr]:write(string.format("[%s] %s", time, log.format(...))) end function log.map(...) if not IS_TEST then return end if not log.isWrite("proto") then return end local time = time_now_str() if not mapLogger then local filename = "/map" mapLogger = lib_logger:new(filename) end mapLogger:write(string.format("[%s] %s", time, log.format(...))) end -- 日志输出 function log.raw(filename, cnt, isAddTime) if mapLogHandle[filename] == nil then filename = string.format("/%s", tostring(filename)) mapLogHandle[filename] = lib_logger:new(filename) end if isAddTime then local time = time_now_str() mapLogHandle[filename]:write(string.format("[%s] %s", time, cnt)) else mapLogHandle[filename]:write(cnt) end end function log.debug(...) if not IS_TEST then return end if not log.isWrite("debug") then return end skynet.error("[debug] " .. log.format(...)) end function log.info(...) if not log.isWrite("info") then return end skynet.error("[info] " .. log.format(...)) end function log.warning(...) if not log.isWrite("warning") then return end skynet.error("[warning] " .. log.format(...)) end function log.error(...) if not log.isWrite("warning") then return end skynet.error("[error] " .. log.format(...)) end function log.fatal(...) if not log.isWrite("fatal") then return end skynet.error("[fatal] " .. log.format(...)) end function log.print(...) if not log.isWrite("print") then return end skynet.error(log.format(...)) end function log.printTbl(root) if not log.isWrite("printTbl") then return end if root == nil then return log.info("PRINT_T root is nil") end if type(root) ~= type({}) then return log.info("PRINT_T root not table type") end if not next(root) then return log.info("PRINT_T root is space table") end local cache = {[root] = "."} local function _dump(t, space, name) local temp = {} for k, v in pairs(t) do local key = tostring(k) if cache[v] then table.insert(temp, "+" .. key .. " {" .. cache[v] .. "}") elseif type(v) == "table" then local new_key = name .. "." .. key cache[v] = new_key table.insert(temp, "+" .. key .. _dump(v, space .. (next(t, k) and "|" or " ") .. string.rep(" ", #key), new_key)) else table.insert(temp, "+" .. key .. " [" .. tostring(v) .. "]") end end return table.concat(temp, "\n" .. space) end log.print("\n" .. _dump(root, "", "")) end return log