lua-resty-logger-socket:https://github.com/cloudflare/lua-resty-logger-socket
sylog-ng:https://github.com/syslog-ng/syslog-ng
This library is still experimental and under early development
* lua-resty-logger-socket还处于试验状态
This lua library is a remote logging module for ngx_lua:
This is aimed to replace Nginx's standard ngx_http_log_module to
push access logs to a remote server via an nonblocking socket. A
common remote log server supporting sockets is syslog-ng.
* 这是一个远程日志模块,可用来替换nginx ngx_http_log_module
* 常用的远程日志服务器是syslog-ng
This Lua library takes advantage of ngx_lua's cosocket API, which
ensures 100% nonblocking behavior
* 这个日志模块充分利用了ngx_lua的cossocket api,保证了非阻塞
init:初始化日志
语法格式:ok, err = logger.init(user_config)
Initialize logger with user configurations. This logger must be initted
before use. If you do not initialize the logger, you will get an error
* 使用之前必须先初始化日志,否则会报错
# user_config:用户配置
* flush_limit:缓冲区加上当前内容达到阀值后,刷新到远程日志服务器,默认4096(4kb)
* drop_limit:如果刷新内容超过flush_limit,当前内容需要丢弃,默认1048576 (1MB)
* timeout:超时时间,默认1s
* host:日志服务器主机
* port:日志服务器端口
* sock_type:传输类型,可选值:tcp(默认)、udp
* path:If the log server uses a stream-typed unix domain socket,
path is the socket file path. Note that host/port and path
cannot both be empty. At least one must be supplied
* max_retry_times:连接失败、或者发送失败,最大重试次数
* retry_interval:重试时间间隔,默认100(0.1s)
* pool_size:连接池,默认10
* max_buffer_reuse:缓冲最大重用次数
* periodic_flush:周期性刷新间隔,设置nil可关闭此功能
* ssl:加密连接,默认false
* ssl_verify:加密验证,默认true
* sni_host:Set the hostname to send in SNI and to use when
verifying certificate match
initted:日志是否初始化
语法格式:initted = logger.initted()
Get a boolean value indicating whether this module has
been initted (by calling the init method)
* 返回boolean表示logger是否初始化
log:输出日志信息
语法格式:bytes, err = logger.log(msg)
Log a message. By default, the log message will be buffered in the
logger module until flush_limit is reached in which case the logger
will flush all the buffered messages to remote log server via a socket.
* 输出日志信息
* 默认情况下,达到flush_limit后,会将你缓冲区的日志刷新到远程日志服务器
bytes is the number of bytes that successfully buffered in the logger.
If bytes is nil, err is a string describing what kind of error happens
this time. If bytes is not nil, then err is a previous error message.
err can be nil when bytes is not nil
* bytes是刷新到远程服务器的字节数
* 如果bytes为nil,err是当前错误描述信息
* 如果bytes不为nil,err是前一次错误描述信息
flush:立刻刷新日志
语法格式:bytes, err = logger.flush()
Flushes any buffered messages out to remote immediately.
Usually you do not need to call this manually because
flushing happens automatically when the buffer is full
* 立刻刷新日志
* 可用来手动刷新日志,默认达到阀值会自动刷新日志
创建syslog-ng容器
docker run -it -d --net fixed --ip 172.18.0.2 \
-p 514:514/udp -p 601:601 --name syslog-ng balabit/syslog-ng
default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/local/openresty/nginx/html;
index index.html index.htm;
}
location /test {
log_by_lua_block {
local logger = require "resty.logger.socket"
if not logger.initted() then
local ok, err = logger.init{
host = '172.18.0.2',
port = 601,
}
if not ok then
ngx.log(ngx.ERR, "failed to initialize the logger: ",
err)
return
end
end
local msg = ngx.var.host .. ngx.var.uri;
local bytes, err = logger.log(msg)
ngx.log(ngx.ERR, "远程日志信息 ==> ", msg);
ngx.log(ngx.ERR, "发送字节bytes ==> ", bytes);
if err then
ngx.log(ngx.ERR, "failed to log message: ", err)
return
end
}
content_by_lua_block {
ngx.say("test");
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/openresty/nginx/html;
}
}
创建openresty容器
docker run -it -d --net fixed --ip 172.18.0.100 -p 4000:80 \
-v /Users/huli/lua/openresty/logger/default.conf:/etc/nginx/conf.d/default.conf \
--name open-logger lihu12344/openresty
安装lua-resty-logger-socket
huli@hudeMacBook-Pro ~ % docker exec -it open-logger bash
[root@fe6893782800 /]# cd /usr/local/openresty/bin
# 搜索模块lua-resty-logger-socket
[root@fe6893782800 bin]# opm search lua-resty-logger-socket
doujiang24/lua-resty-kafka Lua kafka client driver for the Openresty based on the cosocket API
JoshuaOliphant/lua-resty-kafka Lua kafka client driver for the Openresty based on the cosocket API
detailyang/lua-resty-rfc5424 It is an implementation of the RFC5424(syslog) in the OpenResty
p0pr0ck5/lua-resty-logger-socket nonblocking remote access logging for Nginx
p0pr0ck5/lua-resty-waf High-performance WAF built on the OpenResty stack
# 安装模块lua-resty-logger-socket
[root@fe6893782800 bin]# opm install p0pr0ck5/lua-resty-logger-socket
* Fetching p0pr0ck5/lua-resty-logger-socket
Downloading https://opm.openresty.org/api/pkg/tarball/p0pr0ck5/lua-resty-logger-socket-0.03.opm.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 7423 100 7423 0 0 19478 0 --:--:-- --:--:-- --:--:-- 19482
Package p0pr0ck5/lua-resty-logger-socket 0.03 installed successfully under /usr/local/openresty/site/ .
#查看安装的三方模块
[root@fe6893782800 bin]# opm list
p0pr0ck5/lua-resty-logger-socket 0.03
使用测试
# 调用/test接口
huli@hudeMacBook-Pro ~ % curl localhost:4000/test
test
# 查看open-logger日志
huli@hudeMacBook-Pro ~ % docker logs open-logger
172.18.0.1 - - [24/Jul/2022:13:31:22 +0000] "GET /test HTTP/1.1" 200 15 "-" "curl/7.77.0"
2022/07/24 13:35:06 [error] 7#7: *1 [lua] log_by_lua(default.conf:36):19: 远程日志信息 ==> localhost/test while logging request, client: 172.18.0.1, server: localhost, request: "GET /test HTTP/1.1", host: "localhost:4000"
2022/07/24 13:35:06 [error] 7#7: *1 [lua] log_by_lua(default.conf:36):20: 发送字节bytes ==> 14 while logging request, client: 172.18.0.1, server: localhost, request: "GET /test HTTP/1.1", host: "localhost:4000"
172.18.0.1 - - [24/Jul/2022:13:35:06 +0000] "GET /test HTTP/1.1" 200 15 "-" "curl/7.77.0"