From 223896b4de8c6c7fa3e12429a403f827ab3192e5 Mon Sep 17 00:00:00 2001 From: Xiaochen Wang Date: Wed, 27 Sep 2023 15:27:31 +0800 Subject: [PATCH 1/7] fix(dynamic_log_level): not work for nginx debug log fix https://konghq.atlassian.net/browse/KAG-2644 --- src/ngx_http_lua_kong_log.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ngx_http_lua_kong_log.c b/src/ngx_http_lua_kong_log.c index 8ba12828..5c111694 100644 --- a/src/ngx_http_lua_kong_log.c +++ b/src/ngx_http_lua_kong_log.c @@ -81,6 +81,10 @@ ngx_http_lua_kong_get_dynamic_log_level(ngx_uint_t current_log_level) return current_log_level; } + if (g_dynamic_log_level == NGX_LOG_DEBUG) { + return NGX_LOG_DEBUG_ALL; + } + return g_dynamic_log_level; } From 8747b1a5fdb98a77937a39ce161519121cbfbd76 Mon Sep 17 00:00:00 2001 From: Xiaochen Wang Date: Wed, 27 Sep 2023 16:01:42 +0800 Subject: [PATCH 2/7] chore(dynamic_log_level): fix coding style --- src/ngx_http_lua_kong_log.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/ngx_http_lua_kong_log.c b/src/ngx_http_lua_kong_log.c index 5c111694..f0027d55 100644 --- a/src/ngx_http_lua_kong_log.c +++ b/src/ngx_http_lua_kong_log.c @@ -63,6 +63,11 @@ ngx_http_lua_kong_ffi_set_dynamic_log_level(int log_level, int timeout) return NGX_ERROR; } + /* adhere to nginx conventions */ + if (log_level == NGX_LOG_DEBUG) { + log_level = NGX_LOG_DEBUG_ALL; + } + g_dynamic_log_level = log_level; g_dynamic_log_level_timeout_at = (time_t)ngx_time() + (time_t)timeout; @@ -81,15 +86,17 @@ ngx_http_lua_kong_get_dynamic_log_level(ngx_uint_t current_log_level) return current_log_level; } - if (g_dynamic_log_level == NGX_LOG_DEBUG) { - return NGX_LOG_DEBUG_ALL; - } - return g_dynamic_log_level; } int ngx_http_lua_kong_ffi_get_dynamic_log_level(int current_log_level) { - return ngx_http_lua_kong_get_dynamic_log_level(current_log_level); + int log_level = ngx_http_lua_kong_get_dynamic_log_level(current_log_level); + + if (log_level == NGX_LOG_DEBUG_ALL) { + log_level = NGX_LOG_DEBUG; + } + + return log_level; } From a3044d4bd4d9eea20f97031224272aad1632a94d Mon Sep 17 00:00:00 2001 From: Xiaochen Wang Date: Wed, 27 Sep 2023 18:16:20 +0800 Subject: [PATCH 3/7] tests(dynamic_log_level): add test case --- t/008-log.t | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/t/008-log.t b/t/008-log.t index 98bc1003..e6ec158b 100644 --- a/t/008-log.t +++ b/t/008-log.t @@ -938,3 +938,28 @@ GET /test ] --- no_log eval "you can't see me init_worker" + + +=== TEST 25: debug log level for nginx error.log +--- http_config + lua_package_path "../lua-resty-core/lib/?.lua;lualib/?.lua;;"; +--- config + location = /test_nginx_debug_log { + + error_log logs/error.log crit; + + content_by_lua_block { + local log = require("resty.kong.log") + log.set_log_level(ngx.DEBUG, 10) + assert(log.get_log_level(ngx.WARN) == ngx.DEBUG) + ngx.exit(200) + } + } +--- request +GET /test_nginx_debug_log +--- wait: 2 +--- error_code: 200 +--- error_log eval +[ + "http finalize request: 200, "/test_nginx_debug_log?" a:1, c:1", +] From 32b5819cbc626c0a15e1224e0c7e842bc9fc21d9 Mon Sep 17 00:00:00 2001 From: Xiaochen Wang Date: Wed, 27 Sep 2023 18:23:14 +0800 Subject: [PATCH 4/7] fix test case --- t/008-log.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/008-log.t b/t/008-log.t index e6ec158b..2c4e3058 100644 --- a/t/008-log.t +++ b/t/008-log.t @@ -961,5 +961,5 @@ GET /test_nginx_debug_log --- error_code: 200 --- error_log eval [ - "http finalize request: 200, "/test_nginx_debug_log?" a:1, c:1", + qr/[debug] .*: .* http finalize request: 200, "/test_nginx_debug_log?" a:1, c:1/, ] From 4b9901dc1b3f232119e2e5c75b7fa2fdd0fffc2d Mon Sep 17 00:00:00 2001 From: Xiaochen Wang Date: Wed, 27 Sep 2023 18:28:27 +0800 Subject: [PATCH 5/7] fix test case (2) --- t/008-log.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/008-log.t b/t/008-log.t index 2c4e3058..cc9bf48f 100644 --- a/t/008-log.t +++ b/t/008-log.t @@ -961,5 +961,5 @@ GET /test_nginx_debug_log --- error_code: 200 --- error_log eval [ - qr/[debug] .*: .* http finalize request: 200, "/test_nginx_debug_log?" a:1, c:1/, + qr/\[debug\] .*: .* http finalize request: 200, "\/test_nginx_debug_log\?" a:1, c:1/, ] From 642dca55b42d701b91172a1f2bf8c52946d503e8 Mon Sep 17 00:00:00 2001 From: Xiaochen Wang Date: Wed, 27 Sep 2023 18:36:32 +0800 Subject: [PATCH 6/7] fix test case (3) --- t/008-log.t | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/t/008-log.t b/t/008-log.t index cc9bf48f..1f12ade4 100644 --- a/t/008-log.t +++ b/t/008-log.t @@ -952,14 +952,14 @@ GET /test local log = require("resty.kong.log") log.set_log_level(ngx.DEBUG, 10) assert(log.get_log_level(ngx.WARN) == ngx.DEBUG) - ngx.exit(200) + ngx.exit(403) } } --- request GET /test_nginx_debug_log --- wait: 2 ---- error_code: 200 +--- error_code: 403 --- error_log eval [ - qr/\[debug\] .*: .* http finalize request: 200, "\/test_nginx_debug_log\?" a:1, c:1/, + qr/\[debug\] .*: .* http finalize request: 403, "\/test_nginx_debug_log\?" a:1, c:1/, ] From 2b6803817f7f8b76c72e4d339fafda1732fe5764 Mon Sep 17 00:00:00 2001 From: Xiaochen Wang Date: Wed, 27 Sep 2023 19:49:34 +0800 Subject: [PATCH 7/7] fix test case (4) --- t/008-log.t | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/t/008-log.t b/t/008-log.t index 1f12ade4..9e80bd46 100644 --- a/t/008-log.t +++ b/t/008-log.t @@ -7,7 +7,7 @@ use Test::Nginx::Socket::Lua; #master_process_enabled(1); log_level('warn'); -plan tests => repeat_each() * (blocks() * 4) + 8; +plan tests => repeat_each() * (blocks() * 4) + 6; #no_diff(); #no_long_string(); @@ -957,7 +957,6 @@ GET /test } --- request GET /test_nginx_debug_log ---- wait: 2 --- error_code: 403 --- error_log eval [