Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(pdk): serialize log msg with string.buffer #11811

Merged
merged 3 commits into from
Nov 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions kong/pdk/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down