diff --git a/README.md b/README.md index 94dc1c4..7f5da67 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ Available user configurations are listed as follows: * `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. + the unix socket file path. Note that host/port and path cannot both be empty. At least one must be supplied. * `max_retry_times` diff --git a/lib/resty/logger/socket.lua b/lib/resty/logger/socket.lua index c5b897e..833199a 100644 --- a/lib/resty/logger/socket.lua +++ b/lib/resty/logger/socket.lua @@ -114,7 +114,11 @@ local function _do_connect() ok, err = sock:connect(host, port) end elseif path then - ok, err = sock:connect("unix:" .. path) + if (sock_type == 'udp') then + ok, err = sock:setpeername("unix:" .. path) + else + ok, err = sock:connect("unix:" .. path) + end end if not ok then diff --git a/t/sanity.t b/t/sanity.t index 20d8f9d..eaac946 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -63,7 +63,7 @@ foo -=== TEST 2: small flush_limit, instant flush, unix domain socket +=== TEST 2A: small flush_limit, instant flush, unix domain STREAM socket --- http_config eval: $::HttpConfig --- config location /t { @@ -103,6 +103,46 @@ foo +=== TEST 2B: small flush_limit, instant flush, unix domain DGRAM socket +--- http_config eval: $::HttpConfig +--- config + location /t { + content_by_lua 'ngx.say("foo")'; + log_by_lua ' + collectgarbage() -- to help leak testing + + local logger = require "resty.logger.socket" + if not logger.initted() then + local ok, err = logger.init{ + flush_limit = 1, + path = "$TEST_NGINX_HTML_DIR/logger_test.sock", + sock_type = "udp", + } + if not ok then + ngx.log(ngx.ERR, err) + return + end + end + + local bytes, err = logger.log(ngx.var.request_uri) + if err then + ngx.log(ngx.ERR, err) + end + '; + } +--- request +GET /t?a=1&b=2 +--- wait: 0.1 +--- udp_listen eval: "$ENV{TEST_NGINX_HTML_DIR}/logger_test.sock" +--- udp_reply: +--- no_error_log +[error] +--- udp_query: /t?a=1&b=2 +--- response_body +foo + + + === TEST 3: small flush_limit, instant flush, write a number to remote --- http_config eval: $::HttpConfig --- config