diff --git a/Handler/RotatingFileHandler.php b/Handler/RotatingFileHandler.php index 336e8f4..dc6bbd5 100644 --- a/Handler/RotatingFileHandler.php +++ b/Handler/RotatingFileHandler.php @@ -10,7 +10,7 @@ use Exception; use Magento\Framework\App\Config\ScopeConfigInterface; -use Magento\Framework\Filesystem\DirectoryList; +use Magento\Framework\App\Filesystem\DirectoryList; use Monolog\Handler\HandlerInterface; class RotatingFileHandler implements MagentoHandlerInterface diff --git a/Transport/UdpTransportWrapper.php b/Transport/UdpTransportWrapper.php index bfd24d8..6585e3c 100755 --- a/Transport/UdpTransportWrapper.php +++ b/Transport/UdpTransportWrapper.php @@ -8,13 +8,15 @@ namespace Opengento\Logger\Transport; +use Exception; use Gelf\MessageInterface as Message; use Gelf\Transport\TransportInterface; use Gelf\Transport\UdpTransport; use Gelf\Transport\UdpTransportFactory; use Magento\Framework\App\Config\ScopeConfigInterface; +use Psr\Log\LoggerInterface; -use in_array; +use function in_array; /** * Class UdpTransportWrapper @@ -23,11 +25,21 @@ */ class UdpTransportWrapper implements TransportInterface { + /** + * @var UdpTransportFactory + */ + private $transportFactory; + /** * @var ScopeConfigInterface */ private $scopeConfig; + /** + * @var LoggerInterface + */ + private $logger; + /** * @var string */ @@ -44,34 +56,41 @@ class UdpTransportWrapper implements TransportInterface private $chunkSize; /** - * @var UdpTransport - */ - private $transporter; - - /** - * @var UdpTransportFactory + * @var string[] */ - private $transportFactory; + private $ignoredMessages; /** - * @var string[] + * @var UdpTransport */ - private $ignoredMessages; + private $transporter; public function __construct( UdpTransportFactory $transportFactory, ScopeConfigInterface $scopeConfig, + LoggerInterface $logger, string $hostPath, string $portPath, string $chunkSize = UdpTransport::CHUNK_SIZE_LAN, array $ignoredMessages = [] ) { + $this->transportFactory = $transportFactory; $this->scopeConfig = $scopeConfig; + $this->logger = $logger; $this->hostPath = $hostPath; $this->portPath = $portPath; $this->chunkSize = $chunkSize; - $this->transportFactory = $transportFactory; $this->ignoredMessages = $ignoredMessages; + unset($this->transporter); + } + + public function __get(string $name) + { + if ($name === 'transporter') { + return $this->{$name} = $this->createTransporter(); + } + + return null; } /** @@ -83,26 +102,23 @@ public function __construct( */ public function send(Message $message): int { - if (in_array($message->getShortMessage(), $this->ignoredMessages, true)) { - return 0; + if (!in_array($message->getShortMessage(), $this->ignoredMessages, true)) { + try { + return $this->transporter->send($message); + } catch (Exception $e) { + $this->logger->error($e->getMessage(), $e->getTrace()); + } } - return $this->getTransporter()->send($message); + return 0; } - /** - * @return UdpTransport - */ - private function getTransporter(): UdpTransport + private function createTransporter(): UdpTransport { - if (null === $this->transporter) { - $this->transporter = $this->transportFactory->create([ - 'host' => $this->scopeConfig->getValue($this->hostPath), - 'port' => $this->scopeConfig->getValue($this->portPath), - 'chunkSize' => $this->chunkSize - ]); - } - - return $this->transporter; + return $this->transporter = $this->transportFactory->create([ + 'host' => $this->scopeConfig->getValue($this->hostPath), + 'port' => $this->scopeConfig->getValue($this->portPath), + 'chunkSize' => $this->chunkSize + ]); } -} \ No newline at end of file +}