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);