logger_debug.lua 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. local skynet = require "skynet"
  2. local skynet_error = skynet.error
  3. local string_format = string.format
  4. local table_insert = table.insert
  5. local os_date = os.date
  6. local os_time = os.time
  7. local const_header = { "(T)|", "(D)|", "(I)|", "(W)|", "(E)|", "(F)|" }
  8. local header = const_header
  9. local function print(level, fmt, ...)
  10. skynet_error(
  11. os_date("[%m/%d %X]", os_time()),
  12. header[level],
  13. string_format(fmt, ...))
  14. end
  15. local logger_debug = {}
  16. function logger_debug.label(name)
  17. if name then
  18. header = {}
  19. for _, v in ipairs(const_header) do
  20. table_insert(header, v..name)
  21. end
  22. else
  23. header = const_header
  24. end
  25. end
  26. local function auto_print(level,fmt)
  27. local strs_tab = {}
  28. local temp_tb = {}
  29. local function Save(Obj, floors)
  30. if type(Obj) == "number" or type(Obj) == "string" or type(Obj) == "boolean" then
  31. if type(Obj) == "boolean" then
  32. Obj = Obj and "bool:true" or "false"
  33. end
  34. table_insert(strs_tab,Obj)
  35. if floors == 1 then
  36. table_insert(strs_tab,",")
  37. end
  38. -- table_insert(strs_tab,",\n")
  39. elseif type(Obj) == type({}) then
  40. if not temp_tb[Obj] then
  41. temp_tb[Obj] = 1
  42. local beg_Blank = ""
  43. local end_Blank = ""
  44. for i = 1, floors do
  45. beg_Blank = beg_Blank .. " "
  46. end
  47. for i = 1, floors-1 do
  48. end_Blank = end_Blank .. " "
  49. end
  50. table_insert(strs_tab,"{\n")
  51. for k,v in pairs(Obj) do
  52. if tostring(k) ~= "" and v ~= Obj then
  53. table_insert(strs_tab,beg_Blank)
  54. table_insert(strs_tab,"[\"")
  55. table_insert(strs_tab,tostring(k))
  56. table_insert(strs_tab,"\"] = ")
  57. Save(v, floors + 1)
  58. table_insert(strs_tab,"\n")
  59. end
  60. end
  61. table_insert(strs_tab, end_Blank)
  62. table_insert(strs_tab, "}")
  63. if floors ~= 1 then
  64. table_insert(strs_tab, ",")
  65. end
  66. end
  67. else
  68. table_insert(strs_tab,'\"')
  69. table_insert(strs_tab,type(Obj))
  70. table_insert(strs_tab,'\",')
  71. end
  72. end
  73. -- 调用 Save 方法进行日志的记录
  74. Save(fmt, 1)
  75. local strs = table.concat( strs_tab )
  76. strs = type(fmt)..":"..strs
  77. print(level,strs)
  78. end
  79. local function _logger_debug_interface(fmt, ...)
  80. auto_print(1,fmt)
  81. end
  82. function logger_debug.look(fmt, ...)
  83. -- if true then
  84. -- return --线上不开放
  85. -- end
  86. _logger_debug_interface(fmt, ...)
  87. end
  88. return logger_debug