diff --git a/kong/pdk/log.lua b/kong/pdk/log.lua index a0914e525421..e1cf4892cd8d 100644 --- a/kong/pdk/log.lua +++ b/kong/pdk/log.lua @@ -10,6 +10,7 @@ -- @module kong.log +local buffer = require "string.buffer" local errlog = require "ngx.errlog" local ngx_re = require "ngx.re" local inspect = require "inspect" @@ -137,34 +138,34 @@ end local serializers = { - [1] = function(buf, to_string, ...) - buf[1] = to_string((select(1, ...))) + [1] = function(buf, sep, to_string, ...) + buf:put(to_string((select(1, ...)))) end, - [2] = function(buf, to_string, ...) - buf[1] = to_string((select(1, ...))) - buf[2] = to_string((select(2, ...))) + [2] = function(buf, sep, to_string, ...) + buf:put(to_string((select(1, ...)))):put(sep) + :put(to_string((select(2, ...)))) end, - [3] = function(buf, to_string, ...) - buf[1] = to_string((select(1, ...))) - buf[2] = to_string((select(2, ...))) - buf[3] = to_string((select(3, ...))) + [3] = function(buf, sep, to_string, ...) + buf:put(to_string((select(1, ...)))):put(sep) + :put(to_string((select(2, ...)))):put(sep) + :put(to_string((select(3, ...)))) end, - [4] = function(buf, to_string, ...) - buf[1] = to_string((select(1, ...))) - buf[2] = to_string((select(2, ...))) - buf[3] = to_string((select(3, ...))) - buf[4] = to_string((select(4, ...))) + [4] = function(buf, sep, to_string, ...) + buf:put(to_string((select(1, ...)))):put(sep) + :put(to_string((select(2, ...)))):put(sep) + :put(to_string((select(3, ...)))):put(sep) + :put(to_string((select(4, ...)))) end, - [5] = function(buf, to_string, ...) - buf[1] = to_string((select(1, ...))) - buf[2] = to_string((select(2, ...))) - buf[3] = to_string((select(3, ...))) - buf[4] = to_string((select(4, ...))) - buf[5] = to_string((select(5, ...))) + [5] = function(buf, sep, to_string, ...) + buf:put(to_string((select(1, ...)))):put(sep) + :put(to_string((select(2, ...)))):put(sep) + :put(to_string((select(3, ...)))):put(sep) + :put(to_string((select(4, ...)))):put(sep) + :put(to_string((select(5, ...)))) end, } @@ -282,7 +283,7 @@ local function gen_log_func(lvl_const, imm_buf, to_string, stack_level, sep) to_string = to_string or tostring stack_level = stack_level or 2 - local variadic_buf = {} + local variadic_buf = buffer.new() return function(...) local sys_log_level = nil @@ -320,15 +321,16 @@ local function gen_log_func(lvl_const, imm_buf, to_string, stack_level, sep) end if serializers[n] then - serializers[n](variadic_buf, to_string, ...) + serializers[n](variadic_buf, sep or "" , to_string, ...) else - for i = 1, n do - variadic_buf[i] = to_string((select(i, ...))) + for i = 1, n - 1 do + variadic_buf:put(to_string((select(i, ...)))):put(sep or "") end + variadic_buf:put(to_string((select(n, ...)))) end - local msg = concat(variadic_buf, sep, 1, n) + local msg = variadic_buf:get() for i = 1, imm_buf.n_messages do imm_buf[imm_buf.message_idxs[i]] = msg