From 137b2eb4094908809be1c54e1a18287b306438e4 Mon Sep 17 00:00:00 2001 From: "Yichun Zhang (agentzh)" Date: Mon, 11 Aug 2014 16:09:48 -0700 Subject: feature: added flush() method. --- README.md | 10 +++++++ lib/resty/logger/socket.lua | 4 ++- t/flush.t | 73 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 t/flush.t diff --git a/README.md b/README.md index b8e4a62..9f91b34 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Table of Contents * [init](#init) * [initted](#initted) * [log](#log) + * [flush](#flush) * [Installation](#installation) * [TODO](#todo) * [Authors](#authors) @@ -145,6 +146,15 @@ Log a message. By default, the log message will be buffered in the logger module [Back to TOC](#table-of-contents) +flush +----- +`syntax: 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. + +[Back to TOC](#table-of-contents) + Installation ============ diff --git a/lib/resty/logger/socket.lua b/lib/resty/logger/socket.lua index 08efaa3..6aa490f 100644 --- a/lib/resty/logger/socket.lua +++ b/lib/resty/logger/socket.lua @@ -28,7 +28,7 @@ if not ok then clear_tab = function(tab) for k, _ in pairs(tab) do tab[k] = nil end end end -local _M = new_tab(0, 4) +local _M = new_tab(0, 5) local is_exiting @@ -418,5 +418,7 @@ function _M.initted() return logger_initted end +_M.flush = _flush + return _M diff --git a/t/flush.t b/t/flush.t new file mode 100644 index 0000000..6fe7cda --- /dev/null +++ b/t/flush.t @@ -0,0 +1,73 @@ +# vim:set ft= ts=4 sw=4 et: + +use Test::Nginx::Socket::Lua; +use Cwd qw(cwd); + +repeat_each(2); + +plan tests => repeat_each() * (blocks() * 5); +our $HtmlDir = html_dir; + +my $pwd = cwd(); + +our $HttpConfig = qq{ + lua_package_path "$pwd/lib/?.lua;;"; + lua_package_cpath "/usr/local/openresty-debug/lualib/?.so;/usr/local/openresty/lualib/?.so;;"; +}; + +$ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; +$ENV{TEST_NGINX_HTML_DIR} = $HtmlDir; + +no_long_string(); + +log_level('debug'); + +run_tests(); + +__DATA__ + +=== TEST 1: flush manually +--- http_config eval: $::HttpConfig +--- config + location /t { + content_by_lua ' + collectgarbage() -- to help leak testing + + local logger = require "resty.logger.socket" + if not logger.initted() then + local ok, err = logger.init{ + host = "127.0.0.1", + port = 29999, + flush_limit = 100, + pool_size = 5, + retry_interval = 1, + timeout = 100, + } + end + + local bytes, err = logger.log("abc") + if err then + ngx.log(ngx.ERR, err) + end + + local bytes, err = logger.log("efg") + if err then + ngx.log(ngx.ERR, err) + end + + logger.flush() + ngx.say("ok") + '; + } +--- request +GET /t?a=1&b=2 +--- wait: 0.1 +--- tcp_listen: 29999 +--- tcp_reply: +--- no_error_log +[error] +--- tcp_query: abcefg +--- tcp_query_len: 6 +--- response_body +ok + -- 1.8.5.2