Files
kupshop/bundles/External/ZNZBundle/Util/ZNZLogger.php
2025-08-02 16:30:27 +02:00

111 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
namespace External\ZNZBundle\Util;
use Doctrine\DBAL\Exception\DeadlockException;
use Doctrine\DBAL\Exception\LockWaitTimeoutException;
use External\ZNZBundle\Exception\ZNZException;
use KupShop\AdminBundle\Util\ActivityLog;
use KupShop\KupShopBundle\Util\Logging\SentryLogger;
use Psr\Log\LoggerInterface;
class ZNZLogger
{
public function __construct(
private readonly LoggerInterface $logger,
private readonly SentryLogger $sentryLogger,
) {
}
public function log(\Throwable $e, ?string $message = null, array $data = [], string $severity = ActivityLog::SEVERITY_ERROR): void
{
if (isLocalDevelopment()) {
throw $e;
}
if (empty($data['exceptionMessage'])) {
$data['exceptionMessage'] = $e->getMessage();
}
// pokud se jedna o ZNZ chybu, tak do activity logu
if ($e instanceof ZNZException) {
$this->handleZNZException($e, $message, $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 activityProcessLog(string $message, callable $fn): void
{
addActivityLog(ActivityLog::SEVERITY_NOTICE, ActivityLog::TYPE_SYNC, '[ZNZ] Spouštím: '.$message);
$fn();
addActivityLog(ActivityLog::SEVERITY_NOTICE, ActivityLog::TYPE_SYNC, '[ZNZ] Dokončeno: '.$message);
}
public function activity(string $message, array $data, string $severity = ActivityLog::SEVERITY_ERROR): void
{
addActivityLog($severity, ActivityLog::TYPE_SYNC, '[ZNZ] '.$message, $data);
}
public function kibana(string $message, array $data): void
{
$this->logger->notice(
'ZNZ: '.$message,
$data
);
}
protected function handleZNZException(ZNZException $e, ?string $message, array $data, string $severity): void
{
if (!empty($e->getMessage())) {
$message = $message.' - '.$e->getMessage();
}
if ($severity === ActivityLog::SEVERITY_ERROR) {
$forceWarning = false;
$previousException = $e->getPrevious();
// pokud je to deadlock, tak to nechci jako error, ale jako warning
if ($previousException instanceof LockWaitTimeoutException || $previousException instanceof DeadlockException) {
$forceWarning = true;
}
$data['previousExceptionType'] = $previousException ? get_class($previousException) : null;
$warningMessages = [mb_strtolower($e->getMessage())];
if ($previousException) {
$warningMessages[] = mb_strtolower($previousException->getMessage());
}
foreach ($warningMessages as $warningMessage) {
if (str_contains($warningMessage, 'nepovedlo se vlozit zaznam do tabcisorg')
|| str_contains($warningMessage, 'deadlock')) {
$forceWarning = true;
}
}
$lowerMessage = mb_strtolower($e->getMessage());
if (str_contains($lowerMessage, 'nepovedlo se vlozit zaznam do tabcisorg')
|| str_contains($lowerMessage, 'deadlock')) {
$forceWarning = true;
}
if ($forceWarning) {
$severity = ActivityLog::SEVERITY_WARNING;
}
}
$this->activity($message, array_merge($data, $e->getData()), $severity);
}
}