openresty 使用 log_by_lua 发送日志到 syslog-ng
2024-10-21 06:13:46
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. 参考资料
最新文章
- 使用LogMaster4Net实现应用程序日志的集中管理
- ajax内调用WCF服务
- [NHibernate]Nullables
- Codeforces Round #277.5 (Div. 2) ABCDF
- UIProgressView(进度条控件)
- c# 基本值类型及其默认值
- auto_ptr, which can release the space automatically
- JAVA集合差异
- Secure CRT 如何连接虚拟机里面的CentOS系统——当主机使用无线网的时候 作者原创 欢迎转载
- Unicode 与 Unicode Transformation Format(UTF,UTF-8 / UTF-16 / UTF-32)
- P1879 [USACO06NOV]玉米田Corn Fields
- HTTP的一些基本概念
- 关于 /bin/bash: warning: setlocale: LC_ALL: cannot change locale (en.US_UTF-8) 问题
- Android 6.0系统动态请求系统相机权限
- 变量计算——强制类型转换的js面试题
- Luogu2612 ZJOI2012 波浪 DP
- freeRTOS中文实用教程3--中断管理之计数信号量
- Nand Flash 驱动框架
- loj SDOI2017数字表格
- linux输入密码的实现