mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 14:38:45 +00:00
feat(server/debug): Added initial implementation of new Logger (#84)
- Logs out to a file w/ relevant meta data - Provides better readability for end user This should help provide better troubleshooting for both development and end users. This also fixes any previous catches that were attempting to print stack traces, causing the [object Object] message to appear. Co-authored-by: Christopher Gjelten <[email protected]>
This commit is contained in:
1 parent
a5491302b6
commit
211576e237
18 files changed
+363
-114
No files matched your search
+48
-37
@@ -8,13 +8,16 @@ import {
|
||||
getUserId,
|
||||
getPassword,
|
||||
} from './db_utils';
|
||||
import { mainLogger } from './sv_logger';
|
||||
|
||||
// we require set mysql_connection_string to be set in the config
|
||||
const mysqlConnectionString = GetConvar(CONNECTION_STRING, null);
|
||||
if (mysqlConnectionString === null) {
|
||||
throw new Error(
|
||||
`No connection string provided. make sure "${CONNECTION_STRING} is set in your config.`
|
||||
const mysqlConnectionString = GetConvar(CONNECTION_STRING, 'none');
|
||||
if (mysqlConnectionString === 'none') {
|
||||
const error = new Error(
|
||||
`No connection string provided. make sure "${CONNECTION_STRING}" is set in your config.`
|
||||
);
|
||||
mainLogger.error(error.message);
|
||||
throw error;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -26,39 +29,45 @@ if (mysqlConnectionString === null) {
|
||||
* https://brouznouf.github.io/fivem-mysql-async/config/ and we need to handle both of them.
|
||||
*/
|
||||
export function generateConnectionPool() {
|
||||
if (mysqlConnectionString.includes('database=')) {
|
||||
// This is checking for this format:
|
||||
// set mysql_connection_string "server=127.0.0.1;database=es_extended;userid=user;password=pass"
|
||||
const config = parseSemiColonFormat(mysqlConnectionString);
|
||||
return mysql.createPool({
|
||||
host: getServerHost(config),
|
||||
user: getUserId(config),
|
||||
port: config.port ? parseInt(config.port) : DEFAULT_PORT,
|
||||
password: getPassword(config),
|
||||
database: config.database,
|
||||
waitForConnections: true,
|
||||
connectionLimit: 10,
|
||||
queueLimit: 0,
|
||||
});
|
||||
} else {
|
||||
// This is checking for this format:
|
||||
// set mysql_connection_string "mysql://root:[email protected]/es_extended?charset=utf8mb4"
|
||||
const connectionStringParser = new ConnectionStringParser({
|
||||
scheme: 'mysql',
|
||||
hosts: [],
|
||||
});
|
||||
const connectionOjbect = connectionStringParser.parse(
|
||||
mysqlConnectionString
|
||||
);
|
||||
try {
|
||||
if (mysqlConnectionString.includes('database=')) {
|
||||
// This is checking for this format:
|
||||
// set mysql_connection_string "server=127.0.0.1;database=es_extended;userid=user;password=pass"
|
||||
const config = parseSemiColonFormat(mysqlConnectionString);
|
||||
return mysql.createPool({
|
||||
host: getServerHost(config),
|
||||
user: getUserId(config),
|
||||
port: config.port ? parseInt(config.port) : DEFAULT_PORT,
|
||||
password: getPassword(config),
|
||||
database: config.database,
|
||||
waitForConnections: true,
|
||||
connectionLimit: 10,
|
||||
queueLimit: 0,
|
||||
});
|
||||
} else {
|
||||
// This is checking for this format:
|
||||
// set mysql_connection_string "mysql://root:[email protected]/es_extended?charset=utf8mb4"
|
||||
const connectionStringParser = new ConnectionStringParser({
|
||||
scheme: 'mysql',
|
||||
hosts: [],
|
||||
});
|
||||
const connectionOjbect = connectionStringParser.parse(
|
||||
mysqlConnectionString
|
||||
);
|
||||
|
||||
return mysql.createPool({
|
||||
host: connectionOjbect.hosts[0].host,
|
||||
user: connectionOjbect.username,
|
||||
password: connectionOjbect.password,
|
||||
database: connectionOjbect.endpoint,
|
||||
waitForConnections: true,
|
||||
connectionLimit: 10,
|
||||
queueLimit: 0,
|
||||
return mysql.createPool({
|
||||
host: connectionOjbect.hosts[0].host,
|
||||
user: connectionOjbect.username,
|
||||
password: connectionOjbect.password,
|
||||
database: connectionOjbect.endpoint,
|
||||
waitForConnections: true,
|
||||
connectionLimit: 10,
|
||||
queueLimit: 0,
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
mainLogger.error(`SQL Connection Pool Error: ${e.message}`, {
|
||||
connection: mysqlConnectionString,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -75,7 +84,9 @@ export async function withTransaction(queries: Promise<any>[]): Promise<any[]> {
|
||||
await connection.release();
|
||||
return results;
|
||||
} catch (err) {
|
||||
console.warn('Error when submitting queries');
|
||||
mainLogger.warn(
|
||||
`Error when submitting queries in transaction, ${err.message}`
|
||||
);
|
||||
await connection.rollback();
|
||||
await connection.release();
|
||||
return Promise.reject(err);
|
||||
|
||||
Reference in new issue
Block a user