Skip to content

Commit

Permalink
fix(pdk): do not output connection related log trailing with kong.log…
Browse files Browse the repository at this point in the history
….inspect

Signed-off-by: Aapo Talvensaari <[email protected]>
  • Loading branch information
bungle committed Sep 10, 2024
1 parent 94551b7 commit 116c20d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
33 changes: 30 additions & 3 deletions kong/pdk/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ local byte = string.byte


local DOT_BYTE = byte(".")
local FFI_ERROR = require("resty.core.base").FFI_ERROR


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


local ngx_lua_ffi_raw_log do
if ngx.config.subsystem == "http" or ngx.config.is_console then -- luacheck: ignore
ngx_lua_ffi_raw_log = require("ffi").C.ngx_http_lua_ffi_raw_log

elseif ngx.config.subsystem == "stream" then
ngx_lua_ffi_raw_log = require("ffi").C.ngx_stream_lua_ffi_raw_log
end
end


local phases_with_ctx =
phase_checker.new(PHASES.rewrite,
PHASES.access,
Expand Down Expand Up @@ -180,6 +192,21 @@ local serializers = {
end,
}

local function raw_log_inspect(level, msg)
if type(level) ~= "number" then
error("bad argument #1 to 'raw_log' (must be a number)", 2)
end

if type(msg) ~= "string" then
error("bad argument #2 to 'raw_log' (must be a string)", 2)
end

local rc = ngx_lua_ffi_raw_log(nil, level, msg, #msg)
if rc == FFI_ERROR then
error("bad log level", 2)
end
end


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

errlog.raw_log(lvl_const, header)
raw_log_inspect(lvl_const, header)

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

part = part .. (" "):rep(WRAP - #part)
errlog.raw_log(lvl_const, "|" .. part .. "|")
raw_log_inspect(lvl_const, "|" .. part .. "|")

if i > fullmsg_len then
errlog.raw_log(lvl_const, "+" .. ("-"):rep(WRAP) .. "+")
raw_log_inspect(lvl_const, "+" .. ("-"):rep(WRAP) .. "+")
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/01-unit/10-log_serializer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ describe("kong.log.serialize", function()
_G.ngx = {
config = {
subsystem = "stream",
is_console = true,
},
ctx = {
balancer_data = {
Expand Down

0 comments on commit 116c20d

Please sign in to comment.