From 9920ae7ffe6ea79f6ae8de94073cfbb5f4d3539a Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Fri, 9 Aug 2024 13:53:02 +1000 Subject: [PATCH] refactor(logger): dynamically load logger service --- build.js | 5 +---- logger/fivemanage.js | 28 +++++++--------------------- src/database/rawTransaction.ts | 11 ++++++++++- src/logger/index.ts | 14 +++++++++++++- 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/build.js b/build.js index 1e5524b..a7a032d 100644 --- a/build.js +++ b/build.js @@ -37,10 +37,7 @@ dependencies { } client_script 'ui.lua' -server_scripts { - 'dist/build.js', - 'logger/fivemanage.js' -} +server_scripts 'dist/build.js' files { 'web/build/index.html', diff --git a/logger/fivemanage.js b/logger/fivemanage.js index 0119fea..6fb8e36 100644 --- a/logger/fivemanage.js +++ b/logger/fivemanage.js @@ -1,16 +1,17 @@ // https://fivemanage.com/?ref=overextended const apiKey = GetConvar('FIVEMANAGE_LOGS_API_KEY', ''); -const endpoint = 'https://api.fivemanage.com/api/logs/batch'; +if (!apiKey) return console.warning(`convar "FIVEMANAGE_LOGS_API_KEY" has not been set`); + +const batchedLogs = []; +const endpoint = 'https://api.fivemanage.com/api/logs/batch'; const headers = { ['Content-Type']: 'application/json', ['Authorization']: apiKey, ['User-Agent']: 'oxmysql', }; -const batchedLogs = []; - async function sendLogs() { try { const body = JSON.stringify(batchedLogs); @@ -30,23 +31,8 @@ async function sendLogs() { } } -async function logger(level, resource, message, metadata) { - if (!apiKey) return; - +return function logger(data) { if (batchedLogs.length === 0) setTimeout(sendLogs, 500); - batchedLogs.push({ - level: level, - message: message, - resource: resource, - metadata: metadata, - }); -} - -function errorEvent(data) { - delete data.err.sqlMessage; - logger('error', data.resource, `${data.resource} was unable to execute a query!`, data.err); -} - -on('oxmysql:error', errorEvent); -on('oxmysql:transaction-error', errorEvent); + batchedLogs.push(data); +}; diff --git a/src/database/rawTransaction.ts b/src/database/rawTransaction.ts index 14cb046..98b8762 100644 --- a/src/database/rawTransaction.ts +++ b/src/database/rawTransaction.ts @@ -1,5 +1,5 @@ import { getConnection } from './connection'; -import { logError, logQuery } from '../logger'; +import { logError, logger, logQuery } from '../logger'; import { CFXCallback, CFXParameters, TransactionQuery } from '../types'; import { parseTransaction } from '../utils/parseTransaction'; import { setCallback } from '../utils/setCallback'; @@ -69,6 +69,15 @@ export const rawTransaction = async ( err: err, resource: invokingResource, }); + + if (typeof err === 'object' && err.message) delete err.sqlMessage; + + logger({ + level: 'error', + resource: invokingResource, + message: msg, + metadata: err, + }); } if (cb) diff --git a/src/logger/index.ts b/src/logger/index.ts index a14bf16..deaa5af 100644 --- a/src/logger/index.ts +++ b/src/logger/index.ts @@ -2,11 +2,14 @@ import { mysql_debug, mysql_log_size, mysql_slow_query_warning, mysql_ui } from import type { CFXCallback, CFXParameters } from '../types'; import { dbVersion } from '../database'; +const loggerService = GetConvar('mysql_logger_service', ''); +export const logger = new Function(LoadResourceFile('oxmysql', `logger/${loggerService}.js`))() || (() => {}); + export function logError( invokingResource: string, cb: CFXCallback | undefined, isPromise: boolean | undefined, - err: Error | string = '', + err: any | string = '', // i cbf typing the error right now query?: string, parameters?: CFXParameters, includeParameters?: boolean @@ -25,6 +28,15 @@ export function logError( resource: invokingResource, }); + if (typeof err === 'object' && err.message) delete err.sqlMessage; + + logger({ + level: 'error', + resource: invokingResource, + message: message, + metadata: err, + }); + if (cb && isPromise) return cb(null, output); console.error(output);