Buffer and batch Discord log messages
Introduces a queue and timer to batch log messages before sending them to Discord, reducing the number of HTTP requests. Refactors the built-in log handler to use the new queuing system and improves code consistency. Looks gross but works.
This commit is contained in:
1 parent
7f14eef82b
commit
63d2f675f6
1 file changed
+59
-27
+59
-27
@@ -1,19 +1,67 @@
|
|||||||
Logs = Logs or {}
|
Logs = Logs or {}
|
||||||
local BridgeServerConfig = BridgeServerConfig or Require("settings/serverConfig.lua")
|
local BridgeServerConfig = BridgeServerConfig or Require('settings/serverConfig.lua')
|
||||||
|
|
||||||
|
|
||||||
|
local pendingLogs = {}
|
||||||
|
local sendTimer = nil
|
||||||
|
|
||||||
|
local function sendBufferedLogsToDiscord()
|
||||||
|
if #pendingLogs == 0 then
|
||||||
|
sendTimer = nil
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local embeds = {}
|
||||||
|
for _, log in ipairs(pendingLogs) do
|
||||||
|
table.insert(embeds, {
|
||||||
|
color = '15769093',
|
||||||
|
title = log.resource,
|
||||||
|
thumbnail = { url = BridgeServerConfig.EmbedLogo },
|
||||||
|
fields = {
|
||||||
|
{
|
||||||
|
name = 'Log Message',
|
||||||
|
value = '```' .. log.message .. '```',
|
||||||
|
inline = false,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
timestamp = log.timestamp,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
PerformHttpRequest(BridgeServerConfig.WebhookURL, function(err, text, headers) end, 'POST', json.encode({
|
||||||
|
username = 'Community_Bridge Logger',
|
||||||
|
avatar_url = 'https://avatars.githubusercontent.com/u/192999457?s=400&u=da632e8f64c85def390cfd1a73c3b664d6882b38&v=4',
|
||||||
|
embeds = embeds
|
||||||
|
}), { ['Content-Type'] = 'application/json' })
|
||||||
|
|
||||||
|
pendingLogs = {}
|
||||||
|
sendTimer = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local function queueLogForDiscord(message)
|
||||||
|
table.insert(pendingLogs, {
|
||||||
|
message = tostring(message),
|
||||||
|
resource = GetInvokingResource() or GetCurrentResourceName(),
|
||||||
|
timestamp = os.date('!%Y-%m-%dT%H:%M:%S')
|
||||||
|
})
|
||||||
|
|
||||||
|
if not sendTimer then
|
||||||
|
sendTimer = true
|
||||||
|
SetTimeout(3000, sendBufferedLogsToDiscord)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local logHandlers = {
|
local logHandlers = {
|
||||||
fivemerr = function(logData)
|
fivemerr = function(logData)
|
||||||
local fivemerrData = {
|
local fivemerrData = {
|
||||||
level = "info",
|
level = 'info',
|
||||||
message = tostring(logData),
|
message = tostring(logData),
|
||||||
resource = GetInvokingResource() or GetCurrentResourceName(),
|
resource = GetInvokingResource() or GetCurrentResourceName(),
|
||||||
|
|
||||||
}
|
}
|
||||||
PerformHttpRequest("https://api.fivemerr.com/v1/logs", function(code, text, headers)
|
PerformHttpRequest('https://api.fivemerr.com/v1/logs', function(code, text, headers)
|
||||||
if code ~= 200 then
|
if code ~= 200 then
|
||||||
print(("Failed to send log to Fivemerr API (HTTP %d): Please verify your API key is correct and the service is available."):format(code))
|
print(('Failed to send log to Fivemerr API (HTTP %d): Please verify your API key is correct and the service is available.'):format(code))
|
||||||
else
|
|
||||||
print("Log successfully sent to Fivemerr")
|
|
||||||
end
|
end
|
||||||
end, 'POST', json.encode(fivemerrData), {
|
end, 'POST', json.encode(fivemerrData), {
|
||||||
['Content-Type'] = 'application/json',
|
['Content-Type'] = 'application/json',
|
||||||
@@ -29,36 +77,20 @@ local logHandlers = {
|
|||||||
lib.logger(GetInvokingResource(), GetCurrentResourceName(), message)
|
lib.logger(GetInvokingResource(), GetCurrentResourceName(), message)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
["built-in"] = function(message)
|
['built-in'] = function(message)
|
||||||
PerformHttpRequest(BridgeServerConfig.WebhookURL, function(err, text, headers) end, 'POST', json.encode({
|
queueLogForDiscord(message)
|
||||||
username = "Community_Bridge's Logger",
|
|
||||||
avatar_url = 'https://avatars.githubusercontent.com/u/192999457?s=400&u=da632e8f64c85def390cfd1a73c3b664d6882b38&v=4',
|
|
||||||
embeds = { {
|
|
||||||
color = "15769093",
|
|
||||||
title = GetInvokingResource(),
|
|
||||||
thumbnail = { url = BridgeServerConfig.EmbedLogo },
|
|
||||||
fields = {
|
|
||||||
{
|
|
||||||
name = 'Log Message',
|
|
||||||
value = "```" .. message .. "```",
|
|
||||||
inline = false,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
timestamp = os.date('!%Y-%m-%dT%H:%M:%S'),
|
|
||||||
} }
|
|
||||||
}), { ['Content-Type'] = 'application/json' })
|
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
---@deprecated Use Logs.CreateLog instead (deprecated 9/1/25)
|
---@deprecated Use Logs.CreateLog instead (deprecated 9/1/25)
|
||||||
Logs.Send = function(src, message)
|
Logs.Send = function(src, message)
|
||||||
print("^1Deprecated^0 function 'Logs.Send' called. Please use 'Logs.CreateLog' instead.")
|
print('^1Deprecated^0 function Logs.Send called. Please use Logs.CreateLog instead.')
|
||||||
Logs.CreateLog(message)
|
Logs.CreateLog(message)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@deprecated Use Logs.CreateLog instead (deprecated 9/1/25)
|
---@deprecated Use Logs.CreateLog instead (deprecated 9/1/25)
|
||||||
Logs.Notify = function(src, url, image, message)
|
Logs.Notify = function(src, url, image, message)
|
||||||
print("^1Deprecated^0 function 'Logs.Notify' called. Please use 'Logs.CreateLog' instead.")
|
print('^1Deprecated^0 function Logs.Notify called. Please use Logs.CreateLog instead.')
|
||||||
Logs.CreateLog(message)
|
Logs.CreateLog(message)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -68,4 +100,4 @@ Logs.CreateLog = function(logData)
|
|||||||
end
|
end
|
||||||
|
|
||||||
exports('Logs', Logs)
|
exports('Logs', Logs)
|
||||||
return Logs
|
return Logs
|
||||||
Reference in new issue
Block a user