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

fix(pdk): inspect should log at notice level #13642

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions changelog/unreleased/kong/fix-pdk-inspect-notice.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
message: |
Line up the `kong.log.inspect` function to log at `notice` level as documented
in the PDK documentation (used to be `debug`).
type: bugfix
scope: PDK
35 changes: 31 additions & 4 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 Expand Up @@ -534,7 +561,7 @@ do
-- @usage
-- kong.log.inspect.on()
function self.on()
self.print = gen_log_func(_LEVELS.debug, inspect_buf, inspect, 3, " ")
self.print = gen_log_func(_LEVELS.notice, inspect_buf, inspect, 3, " ")
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
Loading