mirror of
https://github.com/getnamingo/registry.git
synced 2025-05-13 16:16:59 +02:00
Further work on better logging
This commit is contained in:
parent
595d10349c
commit
2adce55a07
6 changed files with 69 additions and 78 deletions
|
@ -6,6 +6,7 @@ use Monolog\ErrorHandler;
|
|||
use Monolog\Handler\RotatingFileHandler;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Formatter\LineFormatter;
|
||||
use Monolog\Formatter\HtmlFormatter;
|
||||
use Monolog\Handler\FilterHandler;
|
||||
use Monolog\Handler\WhatFailureGroupHandler;
|
||||
use MonologPHPMailer\PHPMailerHandler;
|
||||
|
@ -68,9 +69,6 @@ class Logger extends \Monolog\Logger
|
|||
$fileHandler->setFormatter($fileFormatter);
|
||||
$this->pushHandler($fileHandler);
|
||||
|
||||
// Archive Old Logs (Move older than 14 days to ZIP)
|
||||
$this->archiveOldLogs($config['logFile']);
|
||||
|
||||
// Pushover Alerts (For CRITICAL, ALERT, EMERGENCY)
|
||||
if (!empty($_ENV['PUSHOVER_KEY'])) {
|
||||
try {
|
||||
|
@ -90,14 +88,14 @@ class Logger extends \Monolog\Logger
|
|||
$mail->SMTPAuth = true;
|
||||
$mail->Username = $_ENV['MAIL_USERNAME'];
|
||||
$mail->Password = $_ENV['MAIL_PASSWORD'];
|
||||
$mail->SMTPSecure = $_ENV['MAIL_ENCRYPTION'];
|
||||
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
||||
$mail->Port = $_ENV['MAIL_PORT'];
|
||||
$mail->setFrom($_ENV['MAIL_FROM_ADDRESS'], $_ENV['MAIL_FROM_NAME']);
|
||||
$mail->addAddress($_ENV['MAIL_FROM_ADDRESS']); // Send to admin email
|
||||
$mail->addAddress($_ENV['MAIL_TO_ADDRESS']); // Send to admin email
|
||||
|
||||
// Attach PHPMailer to Monolog
|
||||
$mailerHandler = new PHPMailerHandler($mail);
|
||||
$mailerHandler->setFormatter(new LineFormatter());
|
||||
$mailerHandler->setFormatter(new HtmlFormatter());
|
||||
|
||||
// Filter Emails to ALERT, CRITICAL, EMERGENCY Only
|
||||
$filteredMailHandler = new FilterHandler($mailerHandler, \Monolog\Logger::ALERT, \Monolog\Logger::EMERGENCY);
|
||||
|
@ -147,47 +145,4 @@ class Logger extends \Monolog\Logger
|
|||
$run->register();
|
||||
}
|
||||
|
||||
/**
|
||||
* Archive Old Logs (Older than 14 Days)
|
||||
*/
|
||||
private function archiveOldLogs($logFilePath)
|
||||
{
|
||||
$logDir = dirname($logFilePath);
|
||||
$backupDir = '/opt/backup';
|
||||
$lockFile = $backupDir . '/log_archive.lock';
|
||||
|
||||
// Prevent multiple processes from running archive at the same time
|
||||
if (file_exists($lockFile)) {
|
||||
return; // Another process is already archiving
|
||||
}
|
||||
touch($lockFile); // Create lock file
|
||||
|
||||
if (!is_dir($backupDir)) {
|
||||
mkdir($backupDir, 0755, true);
|
||||
}
|
||||
|
||||
$logFiles = glob($logDir . '/*.log'); // Get all log files
|
||||
$thresholdDate = strtotime('-14 days'); // Logs older than 14 days
|
||||
|
||||
foreach ($logFiles as $file) {
|
||||
if (filemtime($file) < $thresholdDate) {
|
||||
$filename = basename($file);
|
||||
$monthYear = date('F-Y', filemtime($file));
|
||||
$zipPath = $backupDir . "/logs-{$monthYear}.zip";
|
||||
|
||||
// Open or create ZIP archive
|
||||
$zip = new ZipArchive();
|
||||
if ($zip->open($zipPath, ZipArchive::CREATE) === true) {
|
||||
if (!$zip->locateName($filename)) { // Prevent duplicate addition
|
||||
$zip->addFile($file, $filename);
|
||||
unlink($file); // Delete original log after archiving
|
||||
}
|
||||
$zip->close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unlink($lockFile); // Remove lock when done
|
||||
}
|
||||
|
||||
}
|
|
@ -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]);
|
||||
}
|
|
@ -59,7 +59,17 @@ $server->on('connect', function ($server, $fd) use ($log) {
|
|||
// Register a callback to handle incoming requests
|
||||
$server->on('receive', function ($server, $fd, $reactorId, $data) use ($c, $pool, $log, $rateLimiter) {
|
||||
// Get a PDO connection from the pool
|
||||
try {
|
||||
$pdo = $pool->get();
|
||||
if (!$pdo) {
|
||||
throw new PDOException("Failed to retrieve a connection from Swoole PDOPool.");
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
$log->alert("Swoole PDO Pool failed: " . $e->getMessage());
|
||||
$server->send($fd, "Database failure. Please try again later");
|
||||
$server->close($fd);
|
||||
return;
|
||||
}
|
||||
$domain = trim($data);
|
||||
|
||||
$clientInfo = $server->getClientInfo($fd);
|
||||
|
|
|
@ -100,6 +100,9 @@ $server->handle(function (Connection $conn) use ($table, $pool, $c, $log, $permi
|
|||
try {
|
||||
// Get a PDO connection from the pool
|
||||
$pdo = $pool->get();
|
||||
if (!$pdo) {
|
||||
throw new PDOException("Failed to retrieve a connection from Swoole PDOPool.");
|
||||
}
|
||||
$data = $conn->recv();
|
||||
$connId = spl_object_id($conn);
|
||||
|
||||
|
@ -572,7 +575,7 @@ $server->handle(function (Connection $conn) use ($table, $pool, $c, $log, $permi
|
|||
}
|
||||
} catch (PDOException $e) {
|
||||
// Handle database exceptions
|
||||
$log->error('Database error: ' . $e->getMessage());
|
||||
$log->alert('Database error: ' . $e->getMessage());
|
||||
|
||||
// Here we only retry if it's a *connection* failure.
|
||||
// Common MySQL connection error codes: 2002 (Can't connect), 2006 (Gone away), 2013 (Lost connection).
|
||||
|
|
|
@ -52,7 +52,17 @@ $log->info('server started.');
|
|||
// Handle incoming HTTP requests
|
||||
$http->on('request', function ($request, $response) use ($c, $pool, $log, $rateLimiter) {
|
||||
// Get a PDO connection from the pool
|
||||
try {
|
||||
$pdo = $pool->get();
|
||||
if (!$pdo) {
|
||||
throw new PDOException("Failed to retrieve a connection from Swoole PDOPool.");
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
$log->alert("Swoole PDO Pool failed: " . $e->getMessage());
|
||||
$response->header('Content-Type', 'application/json');
|
||||
$response->status(500);
|
||||
$response->end(json_encode(['error' => 'Database failure. Please try again later.']));
|
||||
}
|
||||
|
||||
$remoteAddr = $request->server['remote_addr'];
|
||||
if (!isIpWhitelisted($remoteAddr, $pdo)) {
|
||||
|
|
|
@ -59,7 +59,17 @@ $server->on('connect', function ($server, $fd) use ($log) {
|
|||
// Register a callback to handle incoming requests
|
||||
$server->on('receive', function ($server, $fd, $reactorId, $data) use ($c, $pool, $log, $rateLimiter) {
|
||||
// Get a PDO connection from the pool
|
||||
try {
|
||||
$pdo = $pool->get();
|
||||
if (!$pdo) {
|
||||
throw new PDOException("Failed to retrieve a connection from Swoole PDOPool.");
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
$log->alert("Swoole PDO Pool failed: " . $e->getMessage());
|
||||
$server->send($fd, "Database failure. Please try again later");
|
||||
$server->close($fd);
|
||||
return;
|
||||
}
|
||||
$privacy = $c['privacy'];
|
||||
$minimum_data = $c['minimum_data'];
|
||||
$parsedQuery = parseQuery($data);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue