openresty 使用 log_by_lua 发送日志到 syslog-ng
1. 安装
opm get p0pr0ck5/lua-resty-logger-socket
2. 使用
location lua_by_lua_block
log_by_lua_block {
local logger = require "resty.logger.socket"
ngx.log(ngx.ERR, "Test Syslog: ", "call")
if not logger.initted() then
local ok, err = logger.init {
host="syslog-ng-host",
port=514,
sock_type="udp",
flush_limit = 1,
--drop_limit = 5678
}
if not ok then
ngx.log(ngx.ERR, "failed to initialize the logger: ", err)
return
end
end
-- construct the custom access log message in
-- the Lua variable "msg"
local url = ngx.var.uri
local method = ngx.req.get_method()
local headers = ngx.req.raw_header(true)
-- tbl
local params_var = ngx.req.get_uri_args()
local client_ip = ngx.var.remote_addr
-- tbl
local user_agent = ngx.req.get_headers()["User-Agent"]
local referer = ngx.req.get_headers()["Referer"] or ""
local cookies = ngx.req.get_headers()["Cookie"]
local bytes, err = logger.log("test")
local bytes, err = logger.log(client_ip.." "..table.concat(params_var).." "..user_agent.." "..referer.." "..cookies.." "..url.." "..method)
if err then
ngx.log(ngx.ERR, "failed to log message: ", err)
return
end
}
3. log server
syslog-ng 其中启用 udp server 同时可以安装对应的后端storage
yum install -y syslog-ng
4. log 查询
syslog-ng server
tail - f /var/log/message
5. 参考资料