Skip to content

Commit

Permalink
feat(resty.kong.log): add more return value for `resty.kong.log.get_l…
Browse files Browse the repository at this point in the history
…og_level()` (#88)

KAG-4258

---------

Co-authored-by: Datong Sun <datong.sun@konghq.com>
  • Loading branch information
chronolaw and dndx authored Apr 17, 2024
1 parent 4e0133a commit 691ba79
Show file tree
Hide file tree
Showing 4 changed files with 333 additions and 65 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,14 +463,16 @@ If we don’t pass timeout to set_log_level(), it will raise a Lua error.

resty.kong.log.get\_log\_level
----------------------------------
**syntax:** *resty.kong.log.get_log_level(level)*
**syntax:** *current_level, timeout, original_level = resty.kong.log.get_log_level()*

**context:** *any*

**subsystems:** *http*

If the dynamic log level is set, it will return the dynamic log level,
otherwise it will return `level`.
Returns the current dynamic log level, remaining timeout and the original log level.

If the dynamic log level is not set or not active,
`timeout` will be `0`.

Please see [Nginx log level constants](https://github.com/openresty/lua-nginx-module#nginx-log-level-constants)
for the possible value of `level`.
Expand Down
26 changes: 21 additions & 5 deletions lualib/resty/kong/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ int
ngx_http_lua_kong_ffi_set_dynamic_log_level(int log_level, int timeout);
int
ngx_http_lua_kong_ffi_get_dynamic_log_level(int current_log_level);
ngx_http_lua_kong_ffi_get_dynamic_log_level(ngx_http_request_t *r,
int *current_log_level, int *timeout, int *original_log_level);
]])


Expand Down Expand Up @@ -57,11 +58,26 @@ function _M.set_log_level(level, timeout)
end


function _M.get_log_level(level)
assert(type(level) == "number", "level must be a number")
assert(math_floor(level) == level, "level must be an integer")
do
local tonumber = tonumber
local get_request = base.get_request

local current_log_level = ffi.new("int[1]")
local original_log_level = ffi.new("int[1]")
local timeout = ffi.new("int[1]")

return tonumber(C.ngx_http_lua_kong_ffi_get_dynamic_log_level(level))
function _M.get_log_level()
local rc = C.ngx_http_lua_kong_ffi_get_dynamic_log_level(get_request(),
current_log_level,
timeout,
original_log_level)
if rc ~= NGX_OK then
error("failed to get dynamic log level: " .. tostring(rc))
end

return tonumber(current_log_level[0]), tonumber(timeout[0]),
tonumber(original_log_level[0])
end
end


Expand Down
46 changes: 40 additions & 6 deletions src/ngx_http_lua_kong_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
**
** Excluding the technical view,
** the Kong Gateway does not supoort the Windows OS.
**
**
*/
#error "The dynamic log level feature is only supported on UNIX like system"
#endif
Expand All @@ -36,7 +36,7 @@
static ngx_uint_t g_dynamic_log_level = NGX_CONF_UNSET_UINT;

/* after this time, the dynamic log level will restore to original value */
static time_t g_dynamic_log_level_timeout_at;
static time_t g_dynamic_log_level_timeout_at;

/* ------------------------------------------------------------------------- */

Expand Down Expand Up @@ -64,28 +64,62 @@ ngx_http_lua_kong_ffi_set_dynamic_log_level(int log_level, int timeout)
}

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

return NGX_OK;
}


ngx_uint_t
ngx_http_lua_kong_get_dynamic_log_level(ngx_uint_t current_log_level)
{
if (g_dynamic_log_level == NGX_CONF_UNSET_UINT) {
return current_log_level;
}

if (g_dynamic_log_level_timeout_at < ngx_time()) {
if (g_dynamic_log_level_timeout_at <= ngx_time()) {
g_dynamic_log_level = NGX_CONF_UNSET_UINT;
return current_log_level;
}

return g_dynamic_log_level;
}


int
ngx_http_lua_kong_ffi_get_dynamic_log_level(int current_log_level)
ngx_http_lua_kong_ffi_get_dynamic_log_level(ngx_http_request_t *r,
int *current_log_level, int *timeout, int *original_log_level)
{
return ngx_http_lua_kong_get_dynamic_log_level(current_log_level);
if (current_log_level == NULL || timeout == NULL || original_log_level == NULL) {
return NGX_ERROR;
}

*original_log_level = r == NULL ?
ngx_cycle->log->log_level:
r->connection->log->log_level;

/* nginx's log_level may be 0x7ffffff0, 0x80000000
* see src/core/ngx_log.h
*/
if (*original_log_level > NGX_LOG_DEBUG) {
*original_log_level = NGX_LOG_DEBUG;
}

/* timeout, disable the dynamic log level */
if (g_dynamic_log_level_timeout_at <= ngx_time()) {
g_dynamic_log_level = NGX_CONF_UNSET_UINT;
}

if (g_dynamic_log_level == NGX_CONF_UNSET_UINT) {
*current_log_level = *original_log_level;
*timeout = 0;

} else {
*current_log_level = g_dynamic_log_level;
*timeout = g_dynamic_log_level_timeout_at - (time_t) ngx_time();
}

ngx_http_lua_kong_assert(*timeout >= 0);

return NGX_OK;
}
Loading

0 comments on commit 691ba79

Please sign in to comment.