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(dynamic_log_level): not work for nginx debug log #69

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 12 additions & 1 deletion src/ngx_http_lua_kong_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor Author

@chobits chobits Sep 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers, we need to adhere nginx conventions here. like the nginx magic, note taht NGX_LOG_DEBUG_ALL is numerically greater than NGX_LOG_DEBUG. This numeric relationship is crucial for the functioning of all code segments that involve log->log_level comparison in Nginx.

// src/core/ngx_log.c

ngx_http_core_error_log
 -> ngx_log_set_log
   -> ngx_log_set_levels
     
static char *
ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log)
{
+-- 52 lines: ngx_uint_t   i, n, d, found;----------------------------------------------------------------------------
    if (log->log_level == NGX_LOG_DEBUG) {
        log->log_level = NGX_LOG_DEBUG_ALL;
    }

    return NGX_CONF_OK;
}

}

g_dynamic_log_level = log_level;
g_dynamic_log_level_timeout_at = (time_t)ngx_time() + (time_t)timeout;

Expand All @@ -87,5 +92,11 @@ ngx_http_lua_kong_get_dynamic_log_level(ngx_uint_t current_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;
}
Copy link
Contributor Author

@chobits chobits Sep 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the caller using ffi call, we need to translate it from 0x7fff fff0 to log level number 0~8.


return log_level;
}
26 changes: 25 additions & 1 deletion t/008-log.t
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -938,3 +938,27 @@ GET /test
]
--- no_log eval
"you can't see me init_worker"


=== TEST 25: debug log level for nginx error.log
chobits marked this conversation as resolved.
Show resolved Hide resolved
--- 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(403)
}
}
--- request
GET /test_nginx_debug_log
--- error_code: 403
--- error_log eval
[
qr/\[debug\] .*: .* http finalize request: 403, "\/test_nginx_debug_log\?" a:1, c:1/,
]
Loading