mirror of
https://github.com/getnamingo/registry.git
synced 2025-05-18 18:39:19 +02:00
Improvements to the logging system
This commit is contained in:
parent
b43483ed2f
commit
15652bad67
12 changed files with 417 additions and 46 deletions
|
@ -13,6 +13,7 @@
|
||||||
"phpmailer/phpmailer": "^6.9",
|
"phpmailer/phpmailer": "^6.9",
|
||||||
"league/plates": "^3.6",
|
"league/plates": "^3.6",
|
||||||
"moneyphp/money": "^4.6",
|
"moneyphp/money": "^4.6",
|
||||||
"utopia-php/messaging": "^0.9.1"
|
"utopia-php/messaging": "^0.9.1",
|
||||||
|
"filips123/monolog-phpmailer": "^2.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,15 @@
|
||||||
require_once 'vendor/autoload.php';
|
require_once 'vendor/autoload.php';
|
||||||
|
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
use MonologPHPMailer\PHPMailerHandler;
|
||||||
|
use Monolog\Formatter\HtmlFormatter;
|
||||||
|
use Monolog\Handler\FilterHandler;
|
||||||
|
use Monolog\Handler\WhatFailureGroupHandler;
|
||||||
use Monolog\Handler\StreamHandler;
|
use Monolog\Handler\StreamHandler;
|
||||||
use Monolog\Handler\RotatingFileHandler;
|
use Monolog\Handler\RotatingFileHandler;
|
||||||
use Monolog\Formatter\LineFormatter;
|
use Monolog\Formatter\LineFormatter;
|
||||||
|
use PHPMailer\PHPMailer\PHPMailer;
|
||||||
|
use PHPMailer\PHPMailer\Exception;
|
||||||
use Ds\Map;
|
use Ds\Map;
|
||||||
use Swoole\Coroutine;
|
use Swoole\Coroutine;
|
||||||
use Swoole\Coroutine\Http\Client;
|
use Swoole\Coroutine\Http\Client;
|
||||||
|
@ -23,32 +29,98 @@ use Money\Exchange\FixedExchange;
|
||||||
* @return Logger
|
* @return Logger
|
||||||
*/
|
*/
|
||||||
function setupLogger($logFilePath, $channelName = 'app') {
|
function setupLogger($logFilePath, $channelName = 'app') {
|
||||||
// Create a log channel
|
|
||||||
$log = new Logger($channelName);
|
$log = new Logger($channelName);
|
||||||
|
|
||||||
// Set up the console handler
|
// Load email & pushover configuration
|
||||||
|
$config = include('/opt/registry/automation/config.php');
|
||||||
|
|
||||||
|
// Console handler (for real-time debugging)
|
||||||
$consoleHandler = new StreamHandler('php://stdout', Logger::DEBUG);
|
$consoleHandler = new StreamHandler('php://stdout', Logger::DEBUG);
|
||||||
$consoleFormatter = new LineFormatter(
|
$consoleFormatter = new LineFormatter(
|
||||||
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
||||||
"Y-m-d H:i:s.u", // Date format
|
"Y-m-d H:i:s.u",
|
||||||
true, // Allow inline line breaks
|
true,
|
||||||
true // Ignore empty context and extra
|
true
|
||||||
);
|
);
|
||||||
$consoleHandler->setFormatter($consoleFormatter);
|
$consoleHandler->setFormatter($consoleFormatter);
|
||||||
$log->pushHandler($consoleHandler);
|
$log->pushHandler($consoleHandler);
|
||||||
|
|
||||||
// Set up the file handler
|
// File handler - Rotates daily, keeps logs for 14 days
|
||||||
$fileHandler = new RotatingFileHandler($logFilePath, 0, Logger::DEBUG);
|
$fileHandler = new RotatingFileHandler($logFilePath, 14, Logger::DEBUG);
|
||||||
$fileFormatter = new LineFormatter(
|
$fileFormatter = new LineFormatter(
|
||||||
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
||||||
"Y-m-d H:i:s.u" // Date format
|
"Y-m-d H:i:s.u"
|
||||||
);
|
);
|
||||||
$fileHandler->setFormatter($fileFormatter);
|
$fileHandler->setFormatter($fileFormatter);
|
||||||
$log->pushHandler($fileHandler);
|
$log->pushHandler($fileHandler);
|
||||||
|
|
||||||
|
// Archive logs older than 14 days
|
||||||
|
archiveOldLogs($logFilePath);
|
||||||
|
|
||||||
|
// Pushover Handler (For CRITICAL, ALERT, EMERGENCY)
|
||||||
|
if (!empty($config['pushover_key'])) {
|
||||||
|
$pushoverHandler = new PushoverHandler($config['pushover_key'], Logger::ALERT);
|
||||||
|
$log->pushHandler($pushoverHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Email Handler (For CRITICAL, ALERT, EMERGENCY)
|
||||||
|
if (!empty($config['mailer_smtp_host'])) {
|
||||||
|
// Create a PHPMailer instance
|
||||||
|
$mail = new PHPMailer(true);
|
||||||
|
try {
|
||||||
|
$mail->isSMTP();
|
||||||
|
$mail->Host = $config['mailer_smtp_host'];
|
||||||
|
$mail->SMTPAuth = true;
|
||||||
|
$mail->Username = $config['mailer_smtp_username'];
|
||||||
|
$mail->Password = $config['mailer_smtp_password'];
|
||||||
|
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
||||||
|
$mail->Port = $config['mailer_smtp_port'];
|
||||||
|
$mail->setFrom($config['mailer_from'], 'Registry System');
|
||||||
|
$mail->addAddress($config['iana_email']);
|
||||||
|
|
||||||
|
// Attach PHPMailer to Monolog
|
||||||
|
$mailerHandler = new PHPMailerHandler($mail);
|
||||||
|
$mailerHandler->setFormatter(new HtmlFormatter);
|
||||||
|
|
||||||
|
$filteredMailHandler = new FilterHandler($mailerHandler, Logger::ALERT, Logger::EMERGENCY);
|
||||||
|
$safeMailHandler = new WhatFailureGroupHandler([$filteredMailHandler]);
|
||||||
|
$log->pushHandler($safeMailHandler);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
error_log("Failed to initialize PHPMailer: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $log;
|
return $log;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function archiveOldLogs($logFilePath) {
|
||||||
|
$logDir = dirname($logFilePath);
|
||||||
|
$backupDir = '/opt/backup';
|
||||||
|
|
||||||
|
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) {
|
||||||
|
$zip->addFile($file, $filename);
|
||||||
|
$zip->close();
|
||||||
|
unlink($file); // Delete original log after archiving
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function fetchCount($pdo, $tableName) {
|
function fetchCount($pdo, $tableName) {
|
||||||
// Calculate the end of the previous day
|
// Calculate the end of the previous day
|
||||||
$endOfPreviousDay = date('Y-m-d 23:59:59', strtotime('-1 day'));
|
$endOfPreviousDay = date('Y-m-d 23:59:59', strtotime('-1 day'));
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
{
|
{
|
||||||
"require": {
|
"require": {
|
||||||
"monolog/monolog": "^3.7",
|
"monolog/monolog": "^3.7",
|
||||||
"namingo/rately": "^0.1.0"
|
"namingo/rately": "^0.1.0",
|
||||||
|
"phpmailer/phpmailer": "^6.9",
|
||||||
|
"filips123/monolog-phpmailer": "^2.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,15 @@
|
||||||
require_once 'vendor/autoload.php';
|
require_once 'vendor/autoload.php';
|
||||||
|
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
use MonologPHPMailer\PHPMailerHandler;
|
||||||
|
use Monolog\Formatter\HtmlFormatter;
|
||||||
|
use Monolog\Handler\FilterHandler;
|
||||||
|
use Monolog\Handler\WhatFailureGroupHandler;
|
||||||
use Monolog\Handler\StreamHandler;
|
use Monolog\Handler\StreamHandler;
|
||||||
use Monolog\Handler\RotatingFileHandler;
|
use Monolog\Handler\RotatingFileHandler;
|
||||||
use Monolog\Formatter\LineFormatter;
|
use Monolog\Formatter\LineFormatter;
|
||||||
|
use PHPMailer\PHPMailer\PHPMailer;
|
||||||
|
use PHPMailer\PHPMailer\Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets up and returns a Logger instance.
|
* Sets up and returns a Logger instance.
|
||||||
|
@ -15,32 +21,98 @@ use Monolog\Formatter\LineFormatter;
|
||||||
* @return Logger
|
* @return Logger
|
||||||
*/
|
*/
|
||||||
function setupLogger($logFilePath, $channelName = 'app') {
|
function setupLogger($logFilePath, $channelName = 'app') {
|
||||||
// Create a log channel
|
|
||||||
$log = new Logger($channelName);
|
$log = new Logger($channelName);
|
||||||
|
|
||||||
// Set up the console handler
|
// Load email & pushover configuration
|
||||||
|
$config = include('/opt/registry/automation/config.php');
|
||||||
|
|
||||||
|
// Console handler (for real-time debugging)
|
||||||
$consoleHandler = new StreamHandler('php://stdout', Logger::DEBUG);
|
$consoleHandler = new StreamHandler('php://stdout', Logger::DEBUG);
|
||||||
$consoleFormatter = new LineFormatter(
|
$consoleFormatter = new LineFormatter(
|
||||||
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
||||||
"Y-m-d H:i:s.u", // Date format
|
"Y-m-d H:i:s.u",
|
||||||
true, // Allow inline line breaks
|
true,
|
||||||
true // Ignore empty context and extra
|
true
|
||||||
);
|
);
|
||||||
$consoleHandler->setFormatter($consoleFormatter);
|
$consoleHandler->setFormatter($consoleFormatter);
|
||||||
$log->pushHandler($consoleHandler);
|
$log->pushHandler($consoleHandler);
|
||||||
|
|
||||||
// Set up the file handler
|
// File handler - Rotates daily, keeps logs for 14 days
|
||||||
$fileHandler = new RotatingFileHandler($logFilePath, 0, Logger::DEBUG);
|
$fileHandler = new RotatingFileHandler($logFilePath, 14, Logger::DEBUG);
|
||||||
$fileFormatter = new LineFormatter(
|
$fileFormatter = new LineFormatter(
|
||||||
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
||||||
"Y-m-d H:i:s.u" // Date format
|
"Y-m-d H:i:s.u"
|
||||||
);
|
);
|
||||||
$fileHandler->setFormatter($fileFormatter);
|
$fileHandler->setFormatter($fileFormatter);
|
||||||
$log->pushHandler($fileHandler);
|
$log->pushHandler($fileHandler);
|
||||||
|
|
||||||
|
// Archive logs older than 14 days
|
||||||
|
archiveOldLogs($logFilePath);
|
||||||
|
|
||||||
|
// Pushover Handler (For CRITICAL, ALERT, EMERGENCY)
|
||||||
|
if (!empty($config['pushover_key'])) {
|
||||||
|
$pushoverHandler = new PushoverHandler($config['pushover_key'], Logger::ALERT);
|
||||||
|
$log->pushHandler($pushoverHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Email Handler (For CRITICAL, ALERT, EMERGENCY)
|
||||||
|
if (!empty($config['mailer_smtp_host'])) {
|
||||||
|
// Create a PHPMailer instance
|
||||||
|
$mail = new PHPMailer(true);
|
||||||
|
try {
|
||||||
|
$mail->isSMTP();
|
||||||
|
$mail->Host = $config['mailer_smtp_host'];
|
||||||
|
$mail->SMTPAuth = true;
|
||||||
|
$mail->Username = $config['mailer_smtp_username'];
|
||||||
|
$mail->Password = $config['mailer_smtp_password'];
|
||||||
|
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
||||||
|
$mail->Port = $config['mailer_smtp_port'];
|
||||||
|
$mail->setFrom($config['mailer_from'], 'Registry System');
|
||||||
|
$mail->addAddress($config['iana_email']);
|
||||||
|
|
||||||
|
// Attach PHPMailer to Monolog
|
||||||
|
$mailerHandler = new PHPMailerHandler($mail);
|
||||||
|
$mailerHandler->setFormatter(new HtmlFormatter);
|
||||||
|
|
||||||
|
$filteredMailHandler = new FilterHandler($mailerHandler, Logger::ALERT, Logger::EMERGENCY);
|
||||||
|
$safeMailHandler = new WhatFailureGroupHandler([$filteredMailHandler]);
|
||||||
|
$log->pushHandler($safeMailHandler);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
error_log("Failed to initialize PHPMailer: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $log;
|
return $log;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function archiveOldLogs($logFilePath) {
|
||||||
|
$logDir = dirname($logFilePath);
|
||||||
|
$backupDir = '/opt/backup';
|
||||||
|
|
||||||
|
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) {
|
||||||
|
$zip->addFile($file, $filename);
|
||||||
|
$zip->close();
|
||||||
|
unlink($file); // Delete original log after archiving
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function isIpWhitelisted($ip, $pdo) {
|
function isIpWhitelisted($ip, $pdo) {
|
||||||
$stmt = $pdo->prepare("SELECT COUNT(*) FROM registrar_whitelist WHERE addr = ?");
|
$stmt = $pdo->prepare("SELECT COUNT(*) FROM registrar_whitelist WHERE addr = ?");
|
||||||
$stmt->execute([$ip]);
|
$stmt->execute([$ip]);
|
||||||
|
|
|
@ -117,7 +117,7 @@ if [[ ("$OS" == "Ubuntu" && "$VER" == "22.04") || ("$OS" == "Ubuntu" && "$VER" =
|
||||||
apt update -y
|
apt update -y
|
||||||
|
|
||||||
echo "Installing PHP and required extensions..."
|
echo "Installing PHP and required extensions..."
|
||||||
apt install -y ${PHP_VERSION} ${PHP_VERSION}-bcmath ${PHP_VERSION}-cli ${PHP_VERSION}-common ${PHP_VERSION}-curl ${PHP_VERSION}-ds ${PHP_VERSION}-fpm ${PHP_VERSION}-gd ${PHP_VERSION}-gmp ${PHP_VERSION}-gnupg ${PHP_VERSION}-igbinary ${PHP_VERSION}-imap ${PHP_VERSION}-intl ${PHP_VERSION}-mbstring ${PHP_VERSION}-opcache ${PHP_VERSION}-readline ${PHP_VERSION}-redis ${PHP_VERSION}-soap ${PHP_VERSION}-swoole ${PHP_VERSION}-uuid ${PHP_VERSION}-xml
|
apt install -y ${PHP_VERSION} ${PHP_VERSION}-bcmath ${PHP_VERSION}-cli ${PHP_VERSION}-common ${PHP_VERSION}-curl ${PHP_VERSION}-ds ${PHP_VERSION}-fpm ${PHP_VERSION}-gd ${PHP_VERSION}-gmp ${PHP_VERSION}-gnupg ${PHP_VERSION}-igbinary ${PHP_VERSION}-imap ${PHP_VERSION}-intl ${PHP_VERSION}-mbstring ${PHP_VERSION}-opcache ${PHP_VERSION}-readline ${PHP_VERSION}-redis ${PHP_VERSION}-soap ${PHP_VERSION}-swoole ${PHP_VERSION}-uuid ${PHP_VERSION}-xml ${PHP_VERSION}-zip
|
||||||
|
|
||||||
# Set timezone to UTC if it's not already
|
# Set timezone to UTC if it's not already
|
||||||
currentTimezone=$(timedatectl status | grep "Time zone" | awk '{print $3}')
|
currentTimezone=$(timedatectl status | grep "Time zone" | awk '{print $3}')
|
||||||
|
|
|
@ -64,6 +64,8 @@ systemctl stop msg_worker
|
||||||
echo "Clearing cache..."
|
echo "Clearing cache..."
|
||||||
php /var/www/cp/bin/clear_cache.php
|
php /var/www/cp/bin/clear_cache.php
|
||||||
|
|
||||||
|
apt install -y php8.3-zip
|
||||||
|
|
||||||
# Clone the new version of the repository
|
# Clone the new version of the repository
|
||||||
echo "Cloning v1.0.16 from the repository..."
|
echo "Cloning v1.0.16 from the repository..."
|
||||||
git clone --branch v1.0.16 --single-branch https://github.com/getnamingo/registry /opt/registry1016
|
git clone --branch v1.0.16 --single-branch https://github.com/getnamingo/registry /opt/registry1016
|
||||||
|
|
|
@ -7,6 +7,8 @@
|
||||||
"league/flysystem": "^3.28",
|
"league/flysystem": "^3.28",
|
||||||
"selective/xmldsig": "^3.1",
|
"selective/xmldsig": "^3.1",
|
||||||
"namingo/rately": "^0.1.0",
|
"namingo/rately": "^0.1.0",
|
||||||
"moneyphp/money": "^4.6"
|
"moneyphp/money": "^4.6",
|
||||||
|
"phpmailer/phpmailer": "^6.9",
|
||||||
|
"filips123/monolog-phpmailer": "^2.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,15 @@
|
||||||
require_once __DIR__ . '/../vendor/autoload.php';
|
require_once __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
use MonologPHPMailer\PHPMailerHandler;
|
||||||
|
use Monolog\Formatter\HtmlFormatter;
|
||||||
|
use Monolog\Handler\FilterHandler;
|
||||||
|
use Monolog\Handler\WhatFailureGroupHandler;
|
||||||
use Monolog\Handler\StreamHandler;
|
use Monolog\Handler\StreamHandler;
|
||||||
use Monolog\Handler\RotatingFileHandler;
|
use Monolog\Handler\RotatingFileHandler;
|
||||||
use Monolog\Formatter\LineFormatter;
|
use Monolog\Formatter\LineFormatter;
|
||||||
|
use PHPMailer\PHPMailer\PHPMailer;
|
||||||
|
use PHPMailer\PHPMailer\Exception;
|
||||||
use Pdp\Domain;
|
use Pdp\Domain;
|
||||||
use Pdp\TopLevelDomains;
|
use Pdp\TopLevelDomains;
|
||||||
use League\Flysystem\Local\LocalFilesystemAdapter;
|
use League\Flysystem\Local\LocalFilesystemAdapter;
|
||||||
|
@ -26,32 +32,98 @@ use Money\Exchange\FixedExchange;
|
||||||
* @return Logger
|
* @return Logger
|
||||||
*/
|
*/
|
||||||
function setupLogger($logFilePath, $channelName = 'app') {
|
function setupLogger($logFilePath, $channelName = 'app') {
|
||||||
// Create a log channel
|
|
||||||
$log = new Logger($channelName);
|
$log = new Logger($channelName);
|
||||||
|
|
||||||
// Set up the console handler
|
// Load email & pushover configuration
|
||||||
|
$config = include('/opt/registry/automation/config.php');
|
||||||
|
|
||||||
|
// Console handler (for real-time debugging)
|
||||||
$consoleHandler = new StreamHandler('php://stdout', Logger::DEBUG);
|
$consoleHandler = new StreamHandler('php://stdout', Logger::DEBUG);
|
||||||
$consoleFormatter = new LineFormatter(
|
$consoleFormatter = new LineFormatter(
|
||||||
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
||||||
"Y-m-d H:i:s.u", // Date format
|
"Y-m-d H:i:s.u",
|
||||||
true, // Allow inline line breaks
|
true,
|
||||||
true // Ignore empty context and extra
|
true
|
||||||
);
|
);
|
||||||
$consoleHandler->setFormatter($consoleFormatter);
|
$consoleHandler->setFormatter($consoleFormatter);
|
||||||
$log->pushHandler($consoleHandler);
|
$log->pushHandler($consoleHandler);
|
||||||
|
|
||||||
// Set up the file handler
|
// File handler - Rotates daily, keeps logs for 14 days
|
||||||
$fileHandler = new RotatingFileHandler($logFilePath, 0, Logger::DEBUG);
|
$fileHandler = new RotatingFileHandler($logFilePath, 14, Logger::DEBUG);
|
||||||
$fileFormatter = new LineFormatter(
|
$fileFormatter = new LineFormatter(
|
||||||
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
||||||
"Y-m-d H:i:s.u" // Date format
|
"Y-m-d H:i:s.u"
|
||||||
);
|
);
|
||||||
$fileHandler->setFormatter($fileFormatter);
|
$fileHandler->setFormatter($fileFormatter);
|
||||||
$log->pushHandler($fileHandler);
|
$log->pushHandler($fileHandler);
|
||||||
|
|
||||||
|
// Archive logs older than 14 days
|
||||||
|
archiveOldLogs($logFilePath);
|
||||||
|
|
||||||
|
// Pushover Handler (For CRITICAL, ALERT, EMERGENCY)
|
||||||
|
if (!empty($config['pushover_key'])) {
|
||||||
|
$pushoverHandler = new PushoverHandler($config['pushover_key'], Logger::ALERT);
|
||||||
|
$log->pushHandler($pushoverHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Email Handler (For CRITICAL, ALERT, EMERGENCY)
|
||||||
|
if (!empty($config['mailer_smtp_host'])) {
|
||||||
|
// Create a PHPMailer instance
|
||||||
|
$mail = new PHPMailer(true);
|
||||||
|
try {
|
||||||
|
$mail->isSMTP();
|
||||||
|
$mail->Host = $config['mailer_smtp_host'];
|
||||||
|
$mail->SMTPAuth = true;
|
||||||
|
$mail->Username = $config['mailer_smtp_username'];
|
||||||
|
$mail->Password = $config['mailer_smtp_password'];
|
||||||
|
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
||||||
|
$mail->Port = $config['mailer_smtp_port'];
|
||||||
|
$mail->setFrom($config['mailer_from'], 'Registry System');
|
||||||
|
$mail->addAddress($config['iana_email']);
|
||||||
|
|
||||||
|
// Attach PHPMailer to Monolog
|
||||||
|
$mailerHandler = new PHPMailerHandler($mail);
|
||||||
|
$mailerHandler->setFormatter(new HtmlFormatter);
|
||||||
|
|
||||||
|
$filteredMailHandler = new FilterHandler($mailerHandler, Logger::ALERT, Logger::EMERGENCY);
|
||||||
|
$safeMailHandler = new WhatFailureGroupHandler([$filteredMailHandler]);
|
||||||
|
$log->pushHandler($safeMailHandler);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
error_log("Failed to initialize PHPMailer: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $log;
|
return $log;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function archiveOldLogs($logFilePath) {
|
||||||
|
$logDir = dirname($logFilePath);
|
||||||
|
$backupDir = '/opt/backup';
|
||||||
|
|
||||||
|
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) {
|
||||||
|
$zip->addFile($file, $filename);
|
||||||
|
$zip->close();
|
||||||
|
unlink($file); // Delete original log after archiving
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function checkLogin($db, $clID, $pw) {
|
function checkLogin($db, $clID, $pw) {
|
||||||
$stmt = $db->prepare("SELECT pw FROM registrar WHERE clid = :username");
|
$stmt = $db->prepare("SELECT pw FROM registrar WHERE clid = :username");
|
||||||
$stmt->execute(['username' => $clID]);
|
$stmt->execute(['username' => $clID]);
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
{
|
{
|
||||||
"require": {
|
"require": {
|
||||||
"monolog/monolog": "^3.7",
|
"monolog/monolog": "^3.7",
|
||||||
"namingo/rately": "^0.1.0"
|
"namingo/rately": "^0.1.0",
|
||||||
|
"phpmailer/phpmailer": "^6.9",
|
||||||
|
"filips123/monolog-phpmailer": "^2.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,15 @@
|
||||||
require_once 'vendor/autoload.php';
|
require_once 'vendor/autoload.php';
|
||||||
|
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
use MonologPHPMailer\PHPMailerHandler;
|
||||||
|
use Monolog\Formatter\HtmlFormatter;
|
||||||
|
use Monolog\Handler\FilterHandler;
|
||||||
|
use Monolog\Handler\WhatFailureGroupHandler;
|
||||||
use Monolog\Handler\StreamHandler;
|
use Monolog\Handler\StreamHandler;
|
||||||
use Monolog\Handler\RotatingFileHandler;
|
use Monolog\Handler\RotatingFileHandler;
|
||||||
use Monolog\Formatter\LineFormatter;
|
use Monolog\Formatter\LineFormatter;
|
||||||
|
use PHPMailer\PHPMailer\PHPMailer;
|
||||||
|
use PHPMailer\PHPMailer\Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets up and returns a Logger instance.
|
* Sets up and returns a Logger instance.
|
||||||
|
@ -15,32 +21,98 @@ use Monolog\Formatter\LineFormatter;
|
||||||
* @return Logger
|
* @return Logger
|
||||||
*/
|
*/
|
||||||
function setupLogger($logFilePath, $channelName = 'app') {
|
function setupLogger($logFilePath, $channelName = 'app') {
|
||||||
// Create a log channel
|
|
||||||
$log = new Logger($channelName);
|
$log = new Logger($channelName);
|
||||||
|
|
||||||
// Set up the console handler
|
// Load email & pushover configuration
|
||||||
|
$config = include('/opt/registry/automation/config.php');
|
||||||
|
|
||||||
|
// Console handler (for real-time debugging)
|
||||||
$consoleHandler = new StreamHandler('php://stdout', Logger::DEBUG);
|
$consoleHandler = new StreamHandler('php://stdout', Logger::DEBUG);
|
||||||
$consoleFormatter = new LineFormatter(
|
$consoleFormatter = new LineFormatter(
|
||||||
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
||||||
"Y-m-d H:i:s.u", // Date format
|
"Y-m-d H:i:s.u",
|
||||||
true, // Allow inline line breaks
|
true,
|
||||||
true // Ignore empty context and extra
|
true
|
||||||
);
|
);
|
||||||
$consoleHandler->setFormatter($consoleFormatter);
|
$consoleHandler->setFormatter($consoleFormatter);
|
||||||
$log->pushHandler($consoleHandler);
|
$log->pushHandler($consoleHandler);
|
||||||
|
|
||||||
// Set up the file handler
|
// File handler - Rotates daily, keeps logs for 14 days
|
||||||
$fileHandler = new RotatingFileHandler($logFilePath, 0, Logger::DEBUG);
|
$fileHandler = new RotatingFileHandler($logFilePath, 14, Logger::DEBUG);
|
||||||
$fileFormatter = new LineFormatter(
|
$fileFormatter = new LineFormatter(
|
||||||
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
||||||
"Y-m-d H:i:s.u" // Date format
|
"Y-m-d H:i:s.u"
|
||||||
);
|
);
|
||||||
$fileHandler->setFormatter($fileFormatter);
|
$fileHandler->setFormatter($fileFormatter);
|
||||||
$log->pushHandler($fileHandler);
|
$log->pushHandler($fileHandler);
|
||||||
|
|
||||||
|
// Archive logs older than 14 days
|
||||||
|
archiveOldLogs($logFilePath);
|
||||||
|
|
||||||
|
// Pushover Handler (For CRITICAL, ALERT, EMERGENCY)
|
||||||
|
if (!empty($config['pushover_key'])) {
|
||||||
|
$pushoverHandler = new PushoverHandler($config['pushover_key'], Logger::ALERT);
|
||||||
|
$log->pushHandler($pushoverHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Email Handler (For CRITICAL, ALERT, EMERGENCY)
|
||||||
|
if (!empty($config['mailer_smtp_host'])) {
|
||||||
|
// Create a PHPMailer instance
|
||||||
|
$mail = new PHPMailer(true);
|
||||||
|
try {
|
||||||
|
$mail->isSMTP();
|
||||||
|
$mail->Host = $config['mailer_smtp_host'];
|
||||||
|
$mail->SMTPAuth = true;
|
||||||
|
$mail->Username = $config['mailer_smtp_username'];
|
||||||
|
$mail->Password = $config['mailer_smtp_password'];
|
||||||
|
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
||||||
|
$mail->Port = $config['mailer_smtp_port'];
|
||||||
|
$mail->setFrom($config['mailer_from'], 'Registry System');
|
||||||
|
$mail->addAddress($config['iana_email']);
|
||||||
|
|
||||||
|
// Attach PHPMailer to Monolog
|
||||||
|
$mailerHandler = new PHPMailerHandler($mail);
|
||||||
|
$mailerHandler->setFormatter(new HtmlFormatter);
|
||||||
|
|
||||||
|
$filteredMailHandler = new FilterHandler($mailerHandler, Logger::ALERT, Logger::EMERGENCY);
|
||||||
|
$safeMailHandler = new WhatFailureGroupHandler([$filteredMailHandler]);
|
||||||
|
$log->pushHandler($safeMailHandler);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
error_log("Failed to initialize PHPMailer: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $log;
|
return $log;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function archiveOldLogs($logFilePath) {
|
||||||
|
$logDir = dirname($logFilePath);
|
||||||
|
$backupDir = '/opt/backup';
|
||||||
|
|
||||||
|
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) {
|
||||||
|
$zip->addFile($file, $filename);
|
||||||
|
$zip->close();
|
||||||
|
unlink($file); // Delete original log after archiving
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function mapContactToVCard($contactDetails, $role, $c) {
|
function mapContactToVCard($contactDetails, $role, $c) {
|
||||||
// Determine which type of disclosure to use
|
// Determine which type of disclosure to use
|
||||||
$disclose_name = ($contactDetails['type'] == 'loc') ? $contactDetails['disclose_name_loc'] : $contactDetails['disclose_name_int'];
|
$disclose_name = ($contactDetails['type'] == 'loc') ? $contactDetails['disclose_name_loc'] : $contactDetails['disclose_name_int'];
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
{
|
{
|
||||||
"require": {
|
"require": {
|
||||||
"monolog/monolog": "^3.7",
|
"monolog/monolog": "^3.7",
|
||||||
"namingo/rately": "^0.1.0"
|
"namingo/rately": "^0.1.0",
|
||||||
|
"phpmailer/phpmailer": "^6.9",
|
||||||
|
"filips123/monolog-phpmailer": "^2.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,15 @@
|
||||||
require_once 'vendor/autoload.php';
|
require_once 'vendor/autoload.php';
|
||||||
|
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
use MonologPHPMailer\PHPMailerHandler;
|
||||||
|
use Monolog\Formatter\HtmlFormatter;
|
||||||
|
use Monolog\Handler\FilterHandler;
|
||||||
|
use Monolog\Handler\WhatFailureGroupHandler;
|
||||||
use Monolog\Handler\StreamHandler;
|
use Monolog\Handler\StreamHandler;
|
||||||
use Monolog\Handler\RotatingFileHandler;
|
use Monolog\Handler\RotatingFileHandler;
|
||||||
use Monolog\Formatter\LineFormatter;
|
use Monolog\Formatter\LineFormatter;
|
||||||
|
use PHPMailer\PHPMailer\PHPMailer;
|
||||||
|
use PHPMailer\PHPMailer\Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets up and returns a Logger instance.
|
* Sets up and returns a Logger instance.
|
||||||
|
@ -15,32 +21,98 @@ use Monolog\Formatter\LineFormatter;
|
||||||
* @return Logger
|
* @return Logger
|
||||||
*/
|
*/
|
||||||
function setupLogger($logFilePath, $channelName = 'app') {
|
function setupLogger($logFilePath, $channelName = 'app') {
|
||||||
// Create a log channel
|
|
||||||
$log = new Logger($channelName);
|
$log = new Logger($channelName);
|
||||||
|
|
||||||
// Set up the console handler
|
// Load email & pushover configuration
|
||||||
|
$config = include('/opt/registry/automation/config.php');
|
||||||
|
|
||||||
|
// Console handler (for real-time debugging)
|
||||||
$consoleHandler = new StreamHandler('php://stdout', Logger::DEBUG);
|
$consoleHandler = new StreamHandler('php://stdout', Logger::DEBUG);
|
||||||
$consoleFormatter = new LineFormatter(
|
$consoleFormatter = new LineFormatter(
|
||||||
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
||||||
"Y-m-d H:i:s.u", // Date format
|
"Y-m-d H:i:s.u",
|
||||||
true, // Allow inline line breaks
|
true,
|
||||||
true // Ignore empty context and extra
|
true
|
||||||
);
|
);
|
||||||
$consoleHandler->setFormatter($consoleFormatter);
|
$consoleHandler->setFormatter($consoleFormatter);
|
||||||
$log->pushHandler($consoleHandler);
|
$log->pushHandler($consoleHandler);
|
||||||
|
|
||||||
// Set up the file handler
|
// File handler - Rotates daily, keeps logs for 14 days
|
||||||
$fileHandler = new RotatingFileHandler($logFilePath, 0, Logger::DEBUG);
|
$fileHandler = new RotatingFileHandler($logFilePath, 14, Logger::DEBUG);
|
||||||
$fileFormatter = new LineFormatter(
|
$fileFormatter = new LineFormatter(
|
||||||
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
||||||
"Y-m-d H:i:s.u" // Date format
|
"Y-m-d H:i:s.u"
|
||||||
);
|
);
|
||||||
$fileHandler->setFormatter($fileFormatter);
|
$fileHandler->setFormatter($fileFormatter);
|
||||||
$log->pushHandler($fileHandler);
|
$log->pushHandler($fileHandler);
|
||||||
|
|
||||||
|
// Archive logs older than 14 days
|
||||||
|
archiveOldLogs($logFilePath);
|
||||||
|
|
||||||
|
// Pushover Handler (For CRITICAL, ALERT, EMERGENCY)
|
||||||
|
if (!empty($config['pushover_key'])) {
|
||||||
|
$pushoverHandler = new PushoverHandler($config['pushover_key'], Logger::ALERT);
|
||||||
|
$log->pushHandler($pushoverHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Email Handler (For CRITICAL, ALERT, EMERGENCY)
|
||||||
|
if (!empty($config['mailer_smtp_host'])) {
|
||||||
|
// Create a PHPMailer instance
|
||||||
|
$mail = new PHPMailer(true);
|
||||||
|
try {
|
||||||
|
$mail->isSMTP();
|
||||||
|
$mail->Host = $config['mailer_smtp_host'];
|
||||||
|
$mail->SMTPAuth = true;
|
||||||
|
$mail->Username = $config['mailer_smtp_username'];
|
||||||
|
$mail->Password = $config['mailer_smtp_password'];
|
||||||
|
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
||||||
|
$mail->Port = $config['mailer_smtp_port'];
|
||||||
|
$mail->setFrom($config['mailer_from'], 'Registry System');
|
||||||
|
$mail->addAddress($config['iana_email']);
|
||||||
|
|
||||||
|
// Attach PHPMailer to Monolog
|
||||||
|
$mailerHandler = new PHPMailerHandler($mail);
|
||||||
|
$mailerHandler->setFormatter(new HtmlFormatter);
|
||||||
|
|
||||||
|
$filteredMailHandler = new FilterHandler($mailerHandler, Logger::ALERT, Logger::EMERGENCY);
|
||||||
|
$safeMailHandler = new WhatFailureGroupHandler([$filteredMailHandler]);
|
||||||
|
$log->pushHandler($safeMailHandler);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
error_log("Failed to initialize PHPMailer: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $log;
|
return $log;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function archiveOldLogs($logFilePath) {
|
||||||
|
$logDir = dirname($logFilePath);
|
||||||
|
$backupDir = '/opt/backup';
|
||||||
|
|
||||||
|
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) {
|
||||||
|
$zip->addFile($file, $filename);
|
||||||
|
$zip->close();
|
||||||
|
unlink($file); // Delete original log after archiving
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function parseQuery($data) {
|
function parseQuery($data) {
|
||||||
$data = trim($data);
|
$data = trim($data);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue