mirror of
https://github.com/getnamingo/registry.git
synced 2025-05-17 18:09:22 +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
|
@ -3,9 +3,15 @@
|
|||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
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\RotatingFileHandler;
|
||||
use Monolog\Formatter\LineFormatter;
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
use PHPMailer\PHPMailer\Exception;
|
||||
use Pdp\Domain;
|
||||
use Pdp\TopLevelDomains;
|
||||
use League\Flysystem\Local\LocalFilesystemAdapter;
|
||||
|
@ -26,32 +32,98 @@ use Money\Exchange\FixedExchange;
|
|||
* @return Logger
|
||||
*/
|
||||
function setupLogger($logFilePath, $channelName = 'app') {
|
||||
// Create a log channel
|
||||
$log = new Logger($channelName);
|
||||
|
||||
// Load email & pushover configuration
|
||||
$config = include('/opt/registry/automation/config.php');
|
||||
|
||||
// Set up the console handler
|
||||
// Console handler (for real-time debugging)
|
||||
$consoleHandler = new StreamHandler('php://stdout', Logger::DEBUG);
|
||||
$consoleFormatter = new LineFormatter(
|
||||
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
|
||||
"Y-m-d H:i:s.u", // Date format
|
||||
true, // Allow inline line breaks
|
||||
true // Ignore empty context and extra
|
||||
"Y-m-d H:i:s.u",
|
||||
true,
|
||||
true
|
||||
);
|
||||
$consoleHandler->setFormatter($consoleFormatter);
|
||||
$log->pushHandler($consoleHandler);
|
||||
|
||||
// Set up the file handler
|
||||
$fileHandler = new RotatingFileHandler($logFilePath, 0, Logger::DEBUG);
|
||||
// File handler - Rotates daily, keeps logs for 14 days
|
||||
$fileHandler = new RotatingFileHandler($logFilePath, 14, Logger::DEBUG);
|
||||
$fileFormatter = new LineFormatter(
|
||||
"[%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);
|
||||
$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;
|
||||
}
|
||||
|
||||
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) {
|
||||
$stmt = $db->prepare("SELECT pw FROM registrar WHERE clid = :username");
|
||||
$stmt->execute(['username' => $clID]);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue