Improve logging performance by conditionally checking trace level

- Introduced a new `isTraceEnabled` function in the logging module to check if trace logging is enabled based on the current log level.
- Updated the `performRequest` function to only log HTTP request details and responses if trace logging is enabled, reducing unnecessary log output and improving performance.

These changes aim to enhance the efficiency of logging during HTTP requests by avoiding excessive logging when not needed.
This commit is contained in:
Joe 2026-03-01 16:43:21 -06:00
parent 33022667dc
commit 25ca0bb27e
2 changed files with 15 additions and 6 deletions

View file

@ -18,11 +18,12 @@ local function performRequest(requestParams)
local requestId = lastRequestId + 1
lastRequestId = requestId
if Log.isTraceEnabled() then
Log.trace("HTTP {}({}) {}", requestParams.Method, requestId, requestParams.Url)
if requestParams.Body ~= nil then
Log.trace("{}", requestParams.Body)
end
end
return Promise.new(function(resolve, reject)
coroutine.wrap(function()
@ -31,7 +32,9 @@ local function performRequest(requestParams)
end)
if success then
if Log.isTraceEnabled() then
Log.trace("Request {} success, response {:#?}", requestId, response)
end
local httpResponse = HttpResponse.fromRobloxResponse(response)
if httpResponse:isSuccess() then
resolve(httpResponse)
@ -39,7 +42,9 @@ local function performRequest(requestParams)
reject(HttpError.fromResponse(httpResponse))
end
else
if Log.isTraceEnabled() then
Log.trace("Request {} failure: {:?}", requestId, response)
end
reject(HttpError.fromRobloxErrorString(response))
end
end)()

View file

@ -29,6 +29,10 @@ function Log.setLogLevelThunk(thunk)
getLogLevel = thunk
end
function Log.isTraceEnabled()
return getLogLevel() >= Level.Trace
end
function Log.trace(template, ...)
if getLogLevel() >= Level.Trace then
print(addTags(TRACE_TAG, Fmt.fmt(template, ...)))