logUtil.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. local skynet = require "skynet"
  2. local lib_logger = require("log.lib_logger")
  3. local table = table
  4. local string, tostring = string, tostring
  5. local pcall, type, next = pcall, type, next
  6. local log = {}
  7. local protoLogger = {}
  8. local mapLogger
  9. local mapLogHandle = {}
  10. -- 对应日志是否要打印:按位判断,所在位是0 不打印,1 打印
  11. function log.isWrite(logName)
  12. -- 正式服不做日志输出
  13. if not IS_TEST then
  14. -- if logName == "warning" then
  15. -- return true
  16. -- end
  17. return false
  18. end
  19. local name = string.upper(logName)
  20. local ctrlName = string.format("%s_LOG", name)
  21. local ok, data = pcall(skynet.getenv, ctrlName)
  22. if ok then
  23. data = tonumber(data)
  24. if data == 1 then
  25. return true
  26. elseif data == 0 then
  27. return false
  28. end
  29. end
  30. local posList = {
  31. INFO = 1,
  32. DEBUG = 2,
  33. PRINT = 3,
  34. PRINTTBL = 4,
  35. FATAL = 5,
  36. WARNING = 6,
  37. ERROR = 7,
  38. PROTO = 8,
  39. CLIENT = 9
  40. }
  41. local pos = posList[name]
  42. if not pos then
  43. return true
  44. end
  45. ok, data = pcall(skynet.getenv, "write_log")
  46. if not ok then
  47. return true
  48. end
  49. local len = data and #data or 0
  50. if pos > len then
  51. return true
  52. end
  53. local flag = tonumber(string.sub(data, pos, pos)) or 0
  54. return flag ~= 0
  55. end
  56. function log.format(...)
  57. local ok, str = pcall(string.format, ...)
  58. if not ok then
  59. return tostring(...)
  60. else
  61. return str
  62. end
  63. end
  64. function log.proto(...)
  65. if not IS_TEST then
  66. return
  67. end
  68. if not log.isWrite("proto") then
  69. return
  70. end
  71. local time = time_now_str()
  72. local dateStr = os.date("%Y-%m-%d", skynet_time())
  73. if protoLogger[dateStr] == nil then
  74. local filename = "/proto-" .. dateStr
  75. protoLogger[dateStr] = lib_logger:new(filename)
  76. end
  77. protoLogger[dateStr]:write(string.format("[%s] %s", time, log.format(...)))
  78. end
  79. function log.map(...)
  80. if not IS_TEST then
  81. return
  82. end
  83. if not log.isWrite("proto") then
  84. return
  85. end
  86. local time = time_now_str()
  87. if not mapLogger then
  88. local filename = "/map"
  89. mapLogger = lib_logger:new(filename)
  90. end
  91. mapLogger:write(string.format("[%s] %s", time, log.format(...)))
  92. end
  93. -- 日志输出
  94. function log.raw(filename, cnt, isAddTime)
  95. if mapLogHandle[filename] == nil then
  96. filename = string.format("/%s", tostring(filename))
  97. mapLogHandle[filename] = lib_logger:new(filename)
  98. end
  99. if isAddTime then
  100. local time = time_now_str()
  101. mapLogHandle[filename]:write(string.format("[%s] %s", time, cnt))
  102. else
  103. mapLogHandle[filename]:write(cnt)
  104. end
  105. end
  106. function log.debug(...)
  107. if not IS_TEST then
  108. return
  109. end
  110. if not log.isWrite("debug") then
  111. return
  112. end
  113. skynet.error("[debug] " .. log.format(...))
  114. end
  115. function log.info(...)
  116. if not log.isWrite("info") then
  117. return
  118. end
  119. skynet.error("[info] " .. log.format(...))
  120. end
  121. function log.warning(...)
  122. if not log.isWrite("warning") then
  123. return
  124. end
  125. skynet.error("[warning] " .. log.format(...))
  126. end
  127. function log.error(...)
  128. if not log.isWrite("warning") then
  129. return
  130. end
  131. skynet.error("[error] " .. log.format(...))
  132. end
  133. function log.fatal(...)
  134. if not log.isWrite("fatal") then
  135. return
  136. end
  137. skynet.error("[fatal] " .. log.format(...))
  138. end
  139. function log.print(...)
  140. if not log.isWrite("print") then
  141. return
  142. end
  143. skynet.error(log.format(...))
  144. end
  145. function log.printTbl(root)
  146. if not log.isWrite("printTbl") then
  147. return
  148. end
  149. if root == nil then
  150. return log.info("PRINT_T root is nil")
  151. end
  152. if type(root) ~= type({}) then
  153. return log.info("PRINT_T root not table type")
  154. end
  155. if not next(root) then
  156. return log.info("PRINT_T root is space table")
  157. end
  158. local cache = {[root] = "."}
  159. local function _dump(t, space, name)
  160. local temp = {}
  161. for k, v in pairs(t) do
  162. local key = tostring(k)
  163. if cache[v] then
  164. table.insert(temp, "+" .. key .. " {" .. cache[v] .. "}")
  165. elseif type(v) == "table" then
  166. local new_key = name .. "." .. key
  167. cache[v] = new_key
  168. table.insert(temp, "+" .. key .. _dump(v, space .. (next(t, k) and "|" or " ") .. string.rep(" ", #key), new_key))
  169. else
  170. table.insert(temp, "+" .. key .. " [" .. tostring(v) .. "]")
  171. end
  172. end
  173. return table.concat(temp, "\n" .. space)
  174. end
  175. log.print("\n" .. _dump(root, "", ""))
  176. end
  177. return log