openresty 使用 log_by_lua 发送日志到 syslog-ng

1. 安装
 
  1. opm get p0pr0ck5/lua-resty-logger-socket
 
2. 使用
   location   lua_by_lua_block 
 
  1. log_by_lua_block {
  2. local logger = require "resty.logger.socket"
  3. ngx.log(ngx.ERR, "Test Syslog: ", "call")
  4. if not logger.initted() then
  5. local ok, err = logger.init {
  6. host="syslog-ng-host",
  7. port=514,
  8. sock_type="udp",
  9. flush_limit = 1,
  10. --drop_limit = 5678
  11. }
  12. if not ok then
  13. ngx.log(ngx.ERR, "failed to initialize the logger: ", err)
  14. return
  15. end
  16. end
  17. -- construct the custom access log message in
  18. -- the Lua variable "msg"
  19. local url = ngx.var.uri
  20. local method = ngx.req.get_method()
  21. local headers = ngx.req.raw_header(true)
  22. -- tbl
  23. local params_var = ngx.req.get_uri_args()
  24. local client_ip = ngx.var.remote_addr
  25. -- tbl
  26. local user_agent = ngx.req.get_headers()["User-Agent"]
  27. local referer = ngx.req.get_headers()["Referer"] or ""
  28. local cookies = ngx.req.get_headers()["Cookie"]
  29. local bytes, err = logger.log("test")
  30. local bytes, err = logger.log(client_ip.." "..table.concat(params_var).." "..user_agent.." "..referer.." "..cookies.." "..url.." "..method)
  31. if err then
  32. ngx.log(ngx.ERR, "failed to log message: ", err)
  33. return
  34. end
  35. }
  
3. log server 
 
     syslog-ng  其中启用  udp  server  同时可以安装对应的后端storage
  1. yum install -y syslog-ng
 
4.  log 查询
     
      syslog-ng  server   
  1. tail - f /var/log/message
 
5. 参考资料
 
 
    
 
 

posted on 2017-06-19 22:36  荣锋亮  阅读(5715)  评论(0编辑  收藏  举报

导航