1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- local skynet = require "skynet"
- require "skynet.manager"
- local fd=nil
- local fname=nil
- local logfolder =nil
- skynet.register_protocol {
- name = "text",
- id = skynet.PTYPE_TEXT,
- unpack = skynet.tostring,
- dispatch = function(_, address, msg)
- if not logfolder then
- return
- end
- local now=os.date("*t",os.time())
- local s=""..now.year..now.month..now.day
- local foldername = string.format("%d_%02d_%02d.log",now.year,now.month,now.day)
- local logpath=logfolder..foldername
- if (not fname) or logpath~=fname then
- if fd then
- fd:close()
- fd=nil
- end
- fname = logpath
- end
- if not fd then
- fd=io.open(logpath,"a+")
- end
- if fd then
- local content = string.format("%s:%08x(%.2f): %s",os.date("%y/%m/%d %H:%M:%S"), address, skynet.time(), msg..'\r\n')
- print(content)
- fd:write(content)
- fd:flush()
- else
- --print(fname)
- end
- end
- }
- skynet.register_protocol {
- name = "SYSTEM",
- id = skynet.PTYPE_SYSTEM,
- unpack = function(...) return ... end,
- dispatch = function()
- if not fd then
- fd:close()
- fname=nil
- end
- end
- }
- skynet.start(function()
- skynet.error("PROJ_ROOT::",PROJ_ROOT)
- logfolder ="../".."/log/"
- local err=os.execute("mkdir "..logfolder)
- skynet.register ".logger"
- end)
|