init.lua 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. -- 日志服务
  2. local skynet = require "skynet"
  3. require "skynet.manager"
  4. local cluster = require "skynet.cluster"
  5. local logger = require "logger"
  6. local util = require "util"
  7. local cjson = require "cjson"
  8. local cjson_encode = cjson.encode
  9. cjson.encode_sparse_array(true, 1)
  10. local skynet_retpack = skynet.retpack
  11. local skynet_pack = skynet.pack
  12. local skynet_redirect = skynet.redirect
  13. local sid -- server id
  14. local logserver
  15. local CMD = {}
  16. function CMD.start()
  17. sid = assert(option.sid)
  18. if option.cluster and option.cluster.log then
  19. logserver = cluster.query("log", "logserver")
  20. end
  21. logger.info("start")
  22. end
  23. function CMD.record(uid, content)
  24. assert(content)
  25. assert(type(content) == "string")
  26. local length = #content
  27. if length > 2048 then
  28. logger.warn("日志内容超过 2048 字节")
  29. end
  30. if logserver then
  31. cluster.send("log", logserver, "record", sid, uid, content)
  32. else
  33. logger.warn("\n%s", content)
  34. end
  35. end
  36. function CMD.http_record(uid, content)
  37. if logserver then
  38. cluster.send("log", logserver, "http_record", sid, uid, content)
  39. else
  40. logger.trace(" http_record 日志发送 %s", cjson_encode(content))
  41. end
  42. end
  43. skynet.init(function()
  44. skynet.register(".log")
  45. end)
  46. skynet.memlimit(128 * 1024 * 1024)
  47. skynet.start(function()
  48. logger.label("<Log>,")
  49. skynet.dispatch("lua", function(session, source, cmd, ...)
  50. local f = assert(CMD[cmd])
  51. if 0==session then
  52. f(...)
  53. else
  54. skynet_retpack(f(...))
  55. end
  56. end)
  57. end)