102 lines
2.7 KiB
PHP
102 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\SynchronizationBundle\Logger;
|
|
|
|
use Doctrine\DBAL\Exception\DeadlockException;
|
|
use Doctrine\DBAL\Exception\LockWaitTimeoutException;
|
|
use KupShop\AdminBundle\Util\ActivityLog;
|
|
use KupShop\KupShopBundle\Util\Logging\CustomDataExceptionInterface;
|
|
use KupShop\KupShopBundle\Util\Logging\SentryLogger;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class SynchronizationLogger
|
|
{
|
|
private ?string $logPrefix = null;
|
|
private array $activityLogExceptions = [];
|
|
|
|
public function __construct(
|
|
private readonly LoggerInterface $logger,
|
|
private readonly SentryLogger $sentryLogger,
|
|
) {
|
|
}
|
|
|
|
public function withLogPrefix(?string $logPrefix): self
|
|
{
|
|
$this->logPrefix = $logPrefix;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function withActivityLogExceptions(array $activityLogExceptions): self
|
|
{
|
|
$this->activityLogExceptions = $activityLogExceptions;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function log(\Throwable $e, ?string $message = null, array $data = [], string $severity = ActivityLog::SEVERITY_ERROR): void
|
|
{
|
|
if (isLocalDevelopment()) {
|
|
throw $e;
|
|
}
|
|
|
|
// chyby, ktere maji jit do activity logu
|
|
if ($this->isActivityLogException($e)) {
|
|
if ($e instanceof CustomDataExceptionInterface) {
|
|
$data = array_merge($data, $e->getData());
|
|
}
|
|
|
|
$this->activity(
|
|
$message ?: "Během synchronizace vznikla chyba: {$e->getMessage()}",
|
|
$data,
|
|
$severity
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
// Lock wait timeout a deadlock nechci logovat
|
|
if ($e instanceof LockWaitTimeoutException || $e instanceof DeadlockException) {
|
|
return;
|
|
}
|
|
|
|
// ostatni chyby do Sentry
|
|
$this->sentryLogger->captureException($e, $data);
|
|
}
|
|
|
|
public function activity(string $message, array $data, string $severity = ActivityLog::SEVERITY_ERROR): int
|
|
{
|
|
return addActivityLog($severity, ActivityLog::TYPE_SYNC, $this->getMessageWithPrefix($message), $data);
|
|
}
|
|
|
|
public function kibana(string $message, array $data): void
|
|
{
|
|
$this->logger->notice(
|
|
$this->getMessageWithPrefix($message),
|
|
$data
|
|
);
|
|
}
|
|
|
|
private function getMessageWithPrefix(string $message): string
|
|
{
|
|
if (!$this->logPrefix) {
|
|
return $message;
|
|
}
|
|
|
|
return "[{$this->logPrefix}] {$message}";
|
|
}
|
|
|
|
private function isActivityLogException(\Throwable $e): bool
|
|
{
|
|
foreach ($this->activityLogExceptions as $class) {
|
|
if (is_a($e, $class)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|