userlog.lua 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. local skynet = require "skynet"
  2. require "skynet.manager"
  3. local fd=nil
  4. local fname=nil
  5. local logfolder =nil
  6. skynet.register_protocol {
  7. name = "text",
  8. id = skynet.PTYPE_TEXT,
  9. unpack = skynet.tostring,
  10. dispatch = function(_, address, msg)
  11. if not logfolder then
  12. return
  13. end
  14. local now=os.date("*t",os.time())
  15. local s=""..now.year..now.month..now.day
  16. local foldername = string.format("%d_%02d_%02d.log",now.year,now.month,now.day)
  17. local logpath=logfolder..foldername
  18. if (not fname) or logpath~=fname then
  19. if fd then
  20. fd:close()
  21. fd=nil
  22. end
  23. fname = logpath
  24. end
  25. if not fd then
  26. fd=io.open(logpath,"a+")
  27. end
  28. if fd then
  29. local content = string.format("%s:%08x(%.2f): %s",os.date("%y/%m/%d %H:%M:%S"), address, skynet.time(), msg..'\r\n')
  30. print(content)
  31. fd:write(content)
  32. fd:flush()
  33. else
  34. --print(fname)
  35. end
  36. end
  37. }
  38. skynet.register_protocol {
  39. name = "SYSTEM",
  40. id = skynet.PTYPE_SYSTEM,
  41. unpack = function(...) return ... end,
  42. dispatch = function()
  43. if not fd then
  44. fd:close()
  45. fname=nil
  46. end
  47. end
  48. }
  49. skynet.start(function()
  50. skynet.error("PROJ_ROOT::",PROJ_ROOT)
  51. logfolder ="../".."/log/"
  52. local err=os.execute("mkdir "..logfolder)
  53. skynet.register ".logger"
  54. end)