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\RotatingFileHandler;
|
||||||
use Monolog\Handler\StreamHandler;
|
use Monolog\Handler\StreamHandler;
|
||||||
use Monolog\Formatter\LineFormatter;
|
use Monolog\Formatter\LineFormatter;
|
||||||
|
use Monolog\Formatter\HtmlFormatter;
|
||||||
use Monolog\Handler\FilterHandler;
|
use Monolog\Handler\FilterHandler;
|
||||||
use Monolog\Handler\WhatFailureGroupHandler;
|
use Monolog\Handler\WhatFailureGroupHandler;
|
||||||
use MonologPHPMailer\PHPMailerHandler;
|
use MonologPHPMailer\PHPMailerHandler;
|
||||||
|
@ -68,9 +69,6 @@ class Logger extends \Monolog\Logger
|
||||||
$fileHandler->setFormatter($fileFormatter);
|
$fileHandler->setFormatter($fileFormatter);
|
||||||
$this->pushHandler($fileHandler);
|
$this->pushHandler($fileHandler);
|
||||||
|
|
||||||
// Archive Old Logs (Move older than 14 days to ZIP)
|
|
||||||
$this->archiveOldLogs($config['logFile']);
|
|
||||||
|
|
||||||
// Pushover Alerts (For CRITICAL, ALERT, EMERGENCY)
|
// Pushover Alerts (For CRITICAL, ALERT, EMERGENCY)
|
||||||
if (!empty($_ENV['PUSHOVER_KEY'])) {
|
if (!empty($_ENV['PUSHOVER_KEY'])) {
|
||||||
try {
|
try {
|
||||||
|
@ -90,14 +88,14 @@ class Logger extends \Monolog\Logger
|
||||||
$mail->SMTPAuth = true;
|
$mail->SMTPAuth = true;
|
||||||
$mail->Username = $_ENV['MAIL_USERNAME'];
|
$mail->Username = $_ENV['MAIL_USERNAME'];
|
||||||
$mail->Password = $_ENV['MAIL_PASSWORD'];
|
$mail->Password = $_ENV['MAIL_PASSWORD'];
|
||||||
$mail->SMTPSecure = $_ENV['MAIL_ENCRYPTION'];
|
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
||||||
$mail->Port = $_ENV['MAIL_PORT'];
|
$mail->Port = $_ENV['MAIL_PORT'];
|
||||||
$mail->setFrom($_ENV['MAIL_FROM_ADDRESS'], $_ENV['MAIL_FROM_NAME']);
|
$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
|
// Attach PHPMailer to Monolog
|
||||||
$mailerHandler = new PHPMailerHandler($mail);
|
$mailerHandler = new PHPMailerHandler($mail);
|
||||||
$mailerHandler->setFormatter(new LineFormatter());
|
$mailerHandler->setFormatter(new HtmlFormatter());
|
||||||
|
|
||||||
// Filter Emails to ALERT, CRITICAL, EMERGENCY Only
|
// Filter Emails to ALERT, CRITICAL, EMERGENCY Only
|
||||||
$filteredMailHandler = new FilterHandler($mailerHandler, \Monolog\Logger::ALERT, \Monolog\Logger::EMERGENCY);
|
$filteredMailHandler = new FilterHandler($mailerHandler, \Monolog\Logger::ALERT, \Monolog\Logger::EMERGENCY);
|
||||||
|
@ -147,47 +145,4 @@ class Logger extends \Monolog\Logger
|
||||||
$run->register();
|
$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
|
<?php
|
||||||
|
|
||||||
|
use Pinga\Db\PdoDatabase;
|
||||||
|
use App\Lib\Logger;
|
||||||
|
|
||||||
|
$log = Logger::getInstance('CP');
|
||||||
|
|
||||||
|
// Load database config
|
||||||
$config = config('connections');
|
$config = config('connections');
|
||||||
// MySQL Connection
|
$defaultDriver = config('default') ?? 'mysql';
|
||||||
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']);
|
$supportedDrivers = [
|
||||||
$db = \Pinga\Db\PdoDatabase::fromPdo($pdo);
|
'mysql' => "{$config['mysql']['driver']}:dbname={$config['mysql']['database']};host={$config['mysql']['host']};charset={$config['mysql']['charset']}",
|
||||||
}
|
'sqlite' => "{$config['sqlite']['driver']}:{$config['sqlite']['driver']}",
|
||||||
// SQLite Connection
|
'pgsql' => "{$config['pgsql']['driver']}:dbname={$config['pgsql']['database']};host={$config['pgsql']['host']}"
|
||||||
elseif (config('default') == 'sqlite') {
|
];
|
||||||
$pdo = new PDO($config['sqlite']['driver'].":".$config['sqlite']['driver']);
|
|
||||||
$db = \Pinga\Db\PdoDatabase::fromPdo($pdo);
|
$pdo = null;
|
||||||
}
|
$db = null;
|
||||||
// PostgreSQL Connection
|
|
||||||
elseif (config('default') == 'pgsql') {
|
try {
|
||||||
$pdo = new \PDO($config['pgsql']['driver'].':dbname='.$config['pgsql']['database'].';host='.$config['pgsql']['host'].';', $config['pgsql']['username'], $config['pgsql']['password']);
|
// Select the correct database driver (fallback to MySQL)
|
||||||
$db = \Pinga\Db\PdoDatabase::fromPdo($pdo);
|
$driver = $supportedDrivers[$defaultDriver] ?? $supportedDrivers['mysql'];
|
||||||
}
|
|
||||||
// SQL Server Connection
|
// Create PDO connection
|
||||||
elseif (config('default') == 'sqlsrv') {
|
$pdo = new \PDO($driver, $config[$defaultDriver]['username'], $config[$defaultDriver]['password']);
|
||||||
$pdo = new PDO($config['sqlsrv']['driver']."sqlsrv:server=".$config['sqlsrv']['host'].";".$config['sqlsrv']['database'], $config['sqlsrv']['username'], $config['sqlsrv']['password']);
|
$db = PdoDatabase::fromPdo($pdo);
|
||||||
$db = \Pinga\Db\PdoDatabase::fromPdo($pdo);
|
|
||||||
}
|
} catch (PDOException $e) {
|
||||||
// MySQL Connection as default
|
$log->alert("Database connection failed: " . $e->getMessage(), ['driver' => $defaultDriver]);
|
||||||
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);
|
|
||||||
}
|
}
|
|
@ -59,7 +59,17 @@ $server->on('connect', function ($server, $fd) use ($log) {
|
||||||
// Register a callback to handle incoming requests
|
// Register a callback to handle incoming requests
|
||||||
$server->on('receive', function ($server, $fd, $reactorId, $data) use ($c, $pool, $log, $rateLimiter) {
|
$server->on('receive', function ($server, $fd, $reactorId, $data) use ($c, $pool, $log, $rateLimiter) {
|
||||||
// Get a PDO connection from the pool
|
// Get a PDO connection from the pool
|
||||||
$pdo = $pool->get();
|
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);
|
$domain = trim($data);
|
||||||
|
|
||||||
$clientInfo = $server->getClientInfo($fd);
|
$clientInfo = $server->getClientInfo($fd);
|
||||||
|
|
|
@ -95,11 +95,14 @@ $server->handle(function (Connection $conn) use ($table, $pool, $c, $log, $permi
|
||||||
|
|
||||||
$log->info('new client from ' . $clientIP . ' connected');
|
$log->info('new client from ' . $clientIP . ' connected');
|
||||||
sendGreeting($conn);
|
sendGreeting($conn);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
// Get a PDO connection from the pool
|
// Get a PDO connection from the pool
|
||||||
$pdo = $pool->get();
|
$pdo = $pool->get();
|
||||||
|
if (!$pdo) {
|
||||||
|
throw new PDOException("Failed to retrieve a connection from Swoole PDOPool.");
|
||||||
|
}
|
||||||
$data = $conn->recv();
|
$data = $conn->recv();
|
||||||
$connId = spl_object_id($conn);
|
$connId = spl_object_id($conn);
|
||||||
|
|
||||||
|
@ -572,7 +575,7 @@ $server->handle(function (Connection $conn) use ($table, $pool, $c, $log, $permi
|
||||||
}
|
}
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
// Handle database exceptions
|
// 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.
|
// 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).
|
// 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
|
// Handle incoming HTTP requests
|
||||||
$http->on('request', function ($request, $response) use ($c, $pool, $log, $rateLimiter) {
|
$http->on('request', function ($request, $response) use ($c, $pool, $log, $rateLimiter) {
|
||||||
// Get a PDO connection from the pool
|
// Get a PDO connection from the pool
|
||||||
$pdo = $pool->get();
|
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'];
|
$remoteAddr = $request->server['remote_addr'];
|
||||||
if (!isIpWhitelisted($remoteAddr, $pdo)) {
|
if (!isIpWhitelisted($remoteAddr, $pdo)) {
|
||||||
|
|
|
@ -59,7 +59,17 @@ $server->on('connect', function ($server, $fd) use ($log) {
|
||||||
// Register a callback to handle incoming requests
|
// Register a callback to handle incoming requests
|
||||||
$server->on('receive', function ($server, $fd, $reactorId, $data) use ($c, $pool, $log, $rateLimiter) {
|
$server->on('receive', function ($server, $fd, $reactorId, $data) use ($c, $pool, $log, $rateLimiter) {
|
||||||
// Get a PDO connection from the pool
|
// Get a PDO connection from the pool
|
||||||
$pdo = $pool->get();
|
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'];
|
$privacy = $c['privacy'];
|
||||||
$minimum_data = $c['minimum_data'];
|
$minimum_data = $c['minimum_data'];
|
||||||
$parsedQuery = parseQuery($data);
|
$parsedQuery = parseQuery($data);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue