12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- -- 日志服务
- local skynet = require "skynet"
- require "skynet.manager"
- local cluster = require "skynet.cluster"
- local logger = require "logger"
- local util = require "util"
- local cjson = require "cjson"
- local cjson_encode = cjson.encode
- cjson.encode_sparse_array(true, 1)
- local skynet_retpack = skynet.retpack
- local skynet_pack = skynet.pack
- local skynet_redirect = skynet.redirect
- local sid -- server id
- local logserver
- local CMD = {}
- function CMD.start()
- sid = assert(option.sid)
- if option.cluster and option.cluster.log then
- logserver = cluster.query("log", "logserver")
- end
- logger.info("start")
- end
- function CMD.record(uid, content)
- assert(content)
- assert(type(content) == "string")
- local length = #content
- if length > 2048 then
- logger.warn("日志内容超过 2048 字节")
- end
- if logserver then
- cluster.send("log", logserver, "record", sid, uid, content)
- else
- logger.warn("\n%s", content)
- end
- end
- function CMD.http_record(uid, content)
- if logserver then
- cluster.send("log", logserver, "http_record", sid, uid, content)
- else
- logger.trace(" http_record 日志发送 %s", cjson_encode(content))
- end
- end
- skynet.init(function()
- skynet.register(".log")
- end)
- skynet.memlimit(128 * 1024 * 1024)
- skynet.start(function()
- logger.label("<Log>,")
- skynet.dispatch("lua", function(session, source, cmd, ...)
- local f = assert(CMD[cmd])
- if 0==session then
- f(...)
- else
- skynet_retpack(f(...))
- end
- end)
- end)
|