116 lines
3.3 KiB
PHP
116 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\VirtualMachine;
|
|
|
|
use App\Http\Services\Telegram;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Throwable;
|
|
|
|
class VmProvisionLogger
|
|
{
|
|
private string $jobName = 'System';
|
|
|
|
private const REDACTED_KEYS = [
|
|
'password',
|
|
'plain_password',
|
|
'guest_password',
|
|
'guestPassword',
|
|
'vcenter_password',
|
|
'vcenterPassword',
|
|
'vcenter_pass',
|
|
'vcenter_user',
|
|
'token',
|
|
'secret',
|
|
];
|
|
|
|
public function __construct(private readonly Telegram $telegram)
|
|
{
|
|
}
|
|
|
|
public function for(string $jobName): static
|
|
{
|
|
$this->jobName = $jobName;
|
|
return $this;
|
|
}
|
|
|
|
public function step(string $title, array $context = []): void
|
|
{
|
|
$safe = $this->redact($context);
|
|
Log::info("[{$this->jobLabel()}] {$title}", $safe);
|
|
$this->telegram->SendJobRunInfo($this->jobLabel(), 'step: ' . $title, $safe);
|
|
}
|
|
|
|
public function success(string $title, array $context = []): void
|
|
{
|
|
$safe = $this->redact($context);
|
|
Log::info("[{$this->jobLabel()}] {$title}", $safe);
|
|
$this->telegram->SendJobRunInfo($this->jobLabel(), 'success: ' . $title, $safe);
|
|
}
|
|
|
|
public function warning(string $title, array $context = []): void
|
|
{
|
|
$safe = $this->redact($context);
|
|
Log::warning("[{$this->jobLabel()}] {$title}", $safe);
|
|
$this->telegram->SendErrorMessage([
|
|
'job' => $this->jobLabel(),
|
|
'level' => 'warning',
|
|
'title' => $title,
|
|
'context' => $safe,
|
|
]);
|
|
}
|
|
|
|
public function error(string $title, array $context = []): void
|
|
{
|
|
$safe = $this->redact($context);
|
|
Log::error("[{$this->jobLabel()}] {$title}", $safe);
|
|
$this->telegram->SendErrorMessage([
|
|
'job' => $this->jobLabel(),
|
|
'level' => 'error',
|
|
'title' => $title,
|
|
'context' => $safe,
|
|
]);
|
|
}
|
|
|
|
public function exception(string $title, Throwable $e, array $context = []): void
|
|
{
|
|
$safe = $this->redact($context);
|
|
Log::error("[{$this->jobLabel()}] {$title}: " . $e->getMessage(), $safe + [
|
|
'exception' => get_class($e),
|
|
'file' => $e->getFile(),
|
|
'line' => $e->getLine(),
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
$this->telegram->SendErrorMessage([
|
|
'job' => $this->jobLabel(),
|
|
'level' => 'exception',
|
|
'title' => $title,
|
|
'error' => $e->getMessage(),
|
|
'file' => $e->getFile(),
|
|
'line' => $e->getLine(),
|
|
'context' => $safe,
|
|
'trace' => substr($e->getTraceAsString(), 0, 500),
|
|
]);
|
|
}
|
|
|
|
private function jobLabel(): string
|
|
{
|
|
return $this->jobName;
|
|
}
|
|
|
|
private function redact(array $context): array
|
|
{
|
|
foreach ($context as $key => $value) {
|
|
if (is_array($value)) {
|
|
$context[$key] = $this->redact($value);
|
|
continue;
|
|
}
|
|
if (in_array(strtolower((string) $key), array_map('strtolower', self::REDACTED_KEYS), true)) {
|
|
$context[$key] = '***';
|
|
}
|
|
}
|
|
return $context;
|
|
}
|
|
}
|