Skip to content

Commit

Permalink
refactor(logger): dynamically load logger service
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Aug 9, 2024
1 parent f005a2f commit 9920ae7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
5 changes: 1 addition & 4 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
28 changes: 7 additions & 21 deletions logger/fivemanage.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
};
11 changes: 10 additions & 1 deletion src/database/rawTransaction.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit 9920ae7

Please sign in to comment.