Skip to content

Commit 4f63f13

Browse files
committed
fix(pdk): do not output connection related log trailing with kong.log.inspect
Signed-off-by: Aapo Talvensaari <[email protected]>
1 parent 274d0fd commit 4f63f13

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

kong/pdk/log.lua

+30-3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ local byte = string.byte
4848

4949

5050
local DOT_BYTE = byte(".")
51+
local FFI_ERROR = require("resty.core.base").FFI_ERROR
5152

5253

5354
local _PREFIX = "[kong] "
@@ -58,6 +59,17 @@ local PHASES_LOG = PHASES.log
5859
local QUESTION_MARK = byte("?")
5960
local TYPE_NAMES = constants.RESPONSE_SOURCE.NAMES
6061

62+
63+
local ngx_lua_ffi_raw_log do
64+
if ngx.config.subsystem == "http" or ngx.config.is_console then -- luacheck: ignore
65+
ngx_lua_ffi_raw_log = require("ffi").C.ngx_http_lua_ffi_raw_log
66+
67+
elseif ngx.config.subsystem == "stream" then
68+
ngx_lua_ffi_raw_log = require("ffi").C.ngx_stream_lua_ffi_raw_log
69+
end
70+
end
71+
72+
6173
local phases_with_ctx =
6274
phase_checker.new(PHASES.rewrite,
6375
PHASES.access,
@@ -180,6 +192,21 @@ local serializers = {
180192
end,
181193
}
182194

195+
local function raw_log_inspect(level, msg)
196+
if type(level) ~= "number" then
197+
error("bad argument #1 to 'raw_log' (must be a number)", 2)
198+
end
199+
200+
if type(msg) ~= "string" then
201+
error("bad argument #2 to 'raw_log' (must be a string)", 2)
202+
end
203+
204+
local rc = ngx_lua_ffi_raw_log(nil, level, msg, #msg)
205+
if rc == FFI_ERROR then
206+
error("bad log level", 2)
207+
end
208+
end
209+
183210

184211
--- Writes a log line to the location specified by the current Nginx
185212
-- configuration block's `error_log` directive, with the `notice` level (similar
@@ -363,7 +390,7 @@ local function gen_log_func(lvl_const, imm_buf, to_string, stack_level, sep)
363390
local i = fullmsg:find("\n") + 1
364391
local header = fullmsg:sub(1, i - 2) .. ("-"):rep(WRAP - i + 3) .. "+"
365392

366-
errlog.raw_log(lvl_const, header)
393+
raw_log_inspect(lvl_const, header)
367394

368395
while i <= fullmsg_len do
369396
local part = sub(fullmsg, i, i + WRAP - 1)
@@ -378,10 +405,10 @@ local function gen_log_func(lvl_const, imm_buf, to_string, stack_level, sep)
378405
end
379406

380407
part = part .. (" "):rep(WRAP - #part)
381-
errlog.raw_log(lvl_const, "|" .. part .. "|")
408+
raw_log_inspect(lvl_const, "|" .. part .. "|")
382409

383410
if i > fullmsg_len then
384-
errlog.raw_log(lvl_const, "+" .. ("-"):rep(WRAP) .. "+")
411+
raw_log_inspect(lvl_const, "+" .. ("-"):rep(WRAP) .. "+")
385412
end
386413
end
387414

spec/01-unit/10-log_serializer_spec.lua

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ describe("kong.log.serialize", function()
253253
_G.ngx = {
254254
config = {
255255
subsystem = "stream",
256+
is_console = true,
256257
},
257258
ctx = {
258259
balancer_data = {

0 commit comments

Comments
 (0)