Further work on better logging

This commit is contained in:
Pinga 2025-02-14 20:50:49 +02:00
parent 595d10349c
commit 2adce55a07
6 changed files with 69 additions and 78 deletions

View file

@ -1,28 +1,31 @@
<?php
use Pinga\Db\PdoDatabase;
use App\Lib\Logger;
$log = Logger::getInstance('CP');
// Load database config
$config = config('connections');
// MySQL Connection
if (config('default') == 'mysql') {
$pdo = new \PDO($config['mysql']['driver'].':dbname='.$config['mysql']['database'].';host='.$config['mysql']['host'].';charset='.$config['mysql']['charset'].'', $config['mysql']['username'], $config['mysql']['password']);
$db = \Pinga\Db\PdoDatabase::fromPdo($pdo);
}
// SQLite Connection
elseif (config('default') == 'sqlite') {
$pdo = new PDO($config['sqlite']['driver'].":".$config['sqlite']['driver']);
$db = \Pinga\Db\PdoDatabase::fromPdo($pdo);
}
// PostgreSQL Connection
elseif (config('default') == 'pgsql') {
$pdo = new \PDO($config['pgsql']['driver'].':dbname='.$config['pgsql']['database'].';host='.$config['pgsql']['host'].';', $config['pgsql']['username'], $config['pgsql']['password']);
$db = \Pinga\Db\PdoDatabase::fromPdo($pdo);
}
// SQL Server Connection
elseif (config('default') == 'sqlsrv') {
$pdo = new PDO($config['sqlsrv']['driver']."sqlsrv:server=".$config['sqlsrv']['host'].";".$config['sqlsrv']['database'], $config['sqlsrv']['username'], $config['sqlsrv']['password']);
$db = \Pinga\Db\PdoDatabase::fromPdo($pdo);
}
// MySQL Connection as default
else{
$pdo = new \PDO($config['mysql']['driver'].':dbname='.$config['mysql']['database'].';host='.$config['mysql']['host'].';charset='.$config['mysql']['charset'].'', $config['mysql']['username'], $config['mysql']['password']);
$db = \Pinga\Db\PdoDatabase::fromPdo($pdo);
$defaultDriver = config('default') ?? 'mysql';
$supportedDrivers = [
'mysql' => "{$config['mysql']['driver']}:dbname={$config['mysql']['database']};host={$config['mysql']['host']};charset={$config['mysql']['charset']}",
'sqlite' => "{$config['sqlite']['driver']}:{$config['sqlite']['driver']}",
'pgsql' => "{$config['pgsql']['driver']}:dbname={$config['pgsql']['database']};host={$config['pgsql']['host']}"
];
$pdo = null;
$db = null;
try {
// Select the correct database driver (fallback to MySQL)
$driver = $supportedDrivers[$defaultDriver] ?? $supportedDrivers['mysql'];
// Create PDO connection
$pdo = new \PDO($driver, $config[$defaultDriver]['username'], $config[$defaultDriver]['password']);
$db = PdoDatabase::fromPdo($pdo);
} catch (PDOException $e) {
$log->alert("Database connection failed: " . $e->getMessage(), ['driver' => $defaultDriver]);
}