-- lua_package_path "/path/to/lua-resty-logger-socket/lib/?.lua;;";
--
-- server {
-- location / {
-- content_by_lua_file lua/log.lua;
-- }
-- }
-- lua/log.lua
local logger = require("resty.logger.socket")
if not logger.initted() then
local ok, err = logger.init{
host = 'xxx',
port = 1234,
flush_limit = 1, --日志长度大于 flush_limit 的时候会将 msg 信息推送一次
drop_limit = 99999,
}
if not ok then
ngx.log(ngx.ERR, "failed to initialize the logger: ", err)
return
end
end
local msg = string.format(.....)
local bytes, err = logger.log(msg)
if err then
ngx.log(ngx.ERR, "failed to log message: ", err)
return
end
function _M.log(msg)
...
if (debug) then
ngx.update_time()
ngx_log(DEBUG, ngx.now(), ":log message length: " .. #msg)
end
local msg_len = #msg
if (is_exiting()) then
exiting = true
_write_buffer(msg)
_flush_buffer()
if (debug) then
ngx_log(DEBUG, "Nginx worker is exiting")
end
bytes = 0
elseif (msg_len + buffer_size < flush_limit) then -- 历史日志大小+本地日志大小小于推送上限
_write_buffer(msg)
bytes = msg_len
elseif (msg_len + buffer_size <= drop_limit) then
_write_buffer(msg)
_flush_buffer()
bytes = msg_len
else
_flush_buffer()
if (debug) then
ngx_log(DEBUG, "logger buffer is full, this log message will be " .. "dropped")
end
bytes = 0
--- this log message doesn't fit in buffer, drop it
...