From e0d4ea6c32e4ef5d8360aa060414f8af7231757f Mon Sep 17 00:00:00 2001 From: darksaid98 Date: Thu, 15 Aug 2024 15:48:11 +0200 Subject: [PATCH] feat(logger): fivemerr support Co-Authored-By: RyanM <2183651+rxnm@users.noreply.github.com> --- logger/fivemerr.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 logger/fivemerr.js diff --git a/logger/fivemerr.js b/logger/fivemerr.js new file mode 100644 index 0000000..551b7f6 --- /dev/null +++ b/logger/fivemerr.js @@ -0,0 +1,40 @@ +// https://fivemerr.com + +const apiKey = GetConvar("FIVEMERR_LOGS_API_KEY", ""); + +if (!apiKey) return console.warning(`convar "FIVEMANAGE_LOGS_API_KEY" has not been set`); + +const batchedLogs = []; +const endpoint = 'https://api.fivemerr.com/v1/logs'; +const headers = { + ['Content-Type']: 'application/json', + ['Authorization']: apiKey, + ['User-Agent']: 'oxmysql', +}; + +async function sendLogs() { + for (let index = 0; index < batchedLogs.length; index++) { + try { + const body = JSON.stringify(batchedLogs[index]); + + const response = await fetch(endpoint, { + method: 'POST', + body: body, + headers: headers, + }); + + if (response.ok) return; + + console.error(`Failed to submit logs to fivemerr - ${response.status} ${response.statusText}`); + } catch (err) { + console.error(err); + } + } + batchedLogs.length = 0; +} + +return function logger(data) { + if (batchedLogs.length === 0) setTimeout(sendLogs, 500); + + batchedLogs.push(data); +};