From 1567e6f3cedd93c10beb71ff5efc1bfab192a77e Mon Sep 17 00:00:00 2001 From: Aapo Talvensaari Date: Tue, 10 Sep 2024 14:49:55 +0300 Subject: [PATCH] fix(pdk): do not output connection related log trailing with kong.log.inspect Signed-off-by: Aapo Talvensaari --- kong/pdk/log.lua | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/kong/pdk/log.lua b/kong/pdk/log.lua index 2aa29456b6132..b4e5686a154ba 100644 --- a/kong/pdk/log.lua +++ b/kong/pdk/log.lua @@ -48,6 +48,7 @@ local byte = string.byte local DOT_BYTE = byte(".") +local FFI_ERROR = require("resty.core.base").FFI_ERROR local _PREFIX = "[kong] " @@ -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" then + 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, @@ -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 @@ -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) @@ -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