From c0419ed5f2e40f4e50cc90274b1b6f76310ecabc Mon Sep 17 00:00:00 2001 From: Anthony Date: Mon, 31 Jan 2022 22:18:07 +0100 Subject: [PATCH 1/4] wrap send message through UDP to handle connection issues --- Publisher/GelfPublisherWrapper.php | 0 Transport/UdpTransportWrapper.php | 7 ++++++- composer.json | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) delete mode 100644 Publisher/GelfPublisherWrapper.php diff --git a/Publisher/GelfPublisherWrapper.php b/Publisher/GelfPublisherWrapper.php deleted file mode 100644 index e69de29..0000000 diff --git a/Transport/UdpTransportWrapper.php b/Transport/UdpTransportWrapper.php index 3708687..9a70040 100755 --- a/Transport/UdpTransportWrapper.php +++ b/Transport/UdpTransportWrapper.php @@ -83,7 +83,12 @@ public function __construct( */ public function send(Message $message): int { - return $this->getTransporter()->send($message); + try { + return $this->getTransporter()->send($message); + } catch (\Exception $e) { + return 0; + } + } /** diff --git a/composer.json b/composer.json index 141dfbb..e174a0f 100755 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "graylog2/gelf-php": "^1.6" }, "type": "magento2-module", - "version": "0.3.0", + "version": "0.3.1", "license": [ "MIT" ], From 0d51838c580daecd1fa51292ade0aec86f4b953a Mon Sep 17 00:00:00 2001 From: Thomas Klein Date: Thu, 24 Mar 2022 20:53:05 +0100 Subject: [PATCH 2/4] Update composer.json + Readme --- README.md | 28 ++++++++++++++++++++++------ composer.json | 14 +++++++++++--- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 00b81e0..8673d6c 100755 --- a/README.md +++ b/README.md @@ -1,6 +1,20 @@ -# Logger by Opengento +# Gelf Logger Module for Magento 2 -Gelf Handler for Magento 2 +[![Latest Stable Version](https://img.shields.io/packagist/v/opengento/logger.svg?style=flat-square)](https://packagist.org/packages/opengento/logger) +[![License: MIT](https://img.shields.io/github/license/opengento/logger.svg?style=flat-square)](./LICENSE) +[![Packagist](https://img.shields.io/packagist/dt/opengento/logger.svg?style=flat-square)](https://packagist.org/packages/opengento/logger/stats) +[![Packagist](https://img.shields.io/packagist/dm/opengento/logger.svg?style=flat-square)](https://packagist.org/packages/opengento/logger/stats) + +This extension sets a new Gelf logger handler for Magento2. + +- [Setup](#setup) +- - [Composer installation](#composer-installation) +- - [Setup the module](#setup-the-module) +- [Features](#features) +- [Settings](#settings) +- [Support](#support) +- [Authors](#authors) +- [License](#license) ## Setup @@ -20,16 +34,18 @@ Run the following magento command: ``` bin/magento setup:upgrade -``` +``` + +**If you are in production mode, do not forget to recompile and redeploy the static resources.** ## Support -Raise a new [request](https://github.com/opengento/getmylog/issues) to the issue tracker. +Raise a new [request](https://github.com/opengento/logger/issues) to the issue tracker. ## Authors -- **Opengento Community** - *Lead* - - [![Twitter Follow](https://img.shields.io/twitter/follow/opengento.svg?style=social)](https://twitter.com/opengento) +- **Opengento Community** - *Lead* - [![Twitter Follow](https://img.shields.io/twitter/follow/opengento.svg?style=social)](https://twitter.com/opengento) +- **Contributors** - *Contributor* - [![GitHub contributors](https://img.shields.io/github/contributors/opengento/logger.svg?style=flat-square)](https://github.com/opengento/logger/graphs/contributors) ## License diff --git a/composer.json b/composer.json index e174a0f..b4106c7 100755 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "opengento/logger", - "description": "Gelf Handler for Magento 2", + "description": "This extension sets a new Gelf logger handler for Magento2.", "keywords": [ "php", "magento", @@ -11,12 +11,16 @@ "Elasticsearch" ], "require": { - "php": ">=7.1", + "php": "^7.1||^8.0", "magento/framework": ">=100.1.0", "graylog2/gelf-php": "^1.6" }, + "require-dev": { + "magento/magento-coding-standard": "^5", + "magento/marketplace-eqp": "^4.0", + "roave/security-advisories": "dev-master" + }, "type": "magento2-module", - "version": "0.3.1", "license": [ "MIT" ], @@ -29,6 +33,10 @@ "role": "lead" } ], + "support": { + "source": "https://github.com/opengento/logger", + "issues": "https://github.com/opengento/logger/issues" + }, "autoload": { "files": [ "registration.php" From 5f924139088204d6c58625c3f8a0b48a11428cbe Mon Sep 17 00:00:00 2001 From: Thomas Klein Date: Thu, 24 Mar 2022 20:54:43 +0100 Subject: [PATCH 3/4] fix markdown syntax --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8673d6c..62810b3 100755 --- a/README.md +++ b/README.md @@ -8,10 +8,8 @@ This extension sets a new Gelf logger handler for Magento2. - [Setup](#setup) -- - [Composer installation](#composer-installation) -- - [Setup the module](#setup-the-module) -- [Features](#features) -- [Settings](#settings) + - [Composer installation](#composer-installation) + - [Setup the module](#setup-the-module) - [Support](#support) - [Authors](#authors) - [License](#license) From 97344300a48fd2b3f860a8cc1bf1e6f1e7e83db1 Mon Sep 17 00:00:00 2001 From: Melvin Capron Date: Thu, 24 Mar 2022 00:42:08 +0100 Subject: [PATCH 4/4] Adding ignored messages configuration for gelf logger. Use case is that PsrFactoryDiscovery send a lot of messages in Gelf which are not real debug exceptions --- Handler/RotatingFileHandler.php | 8 ++- Transport/UdpTransportWrapper.php | 81 ++++++++++++++++++------------- 2 files changed, 54 insertions(+), 35 deletions(-) diff --git a/Handler/RotatingFileHandler.php b/Handler/RotatingFileHandler.php index d091f05..77c488c 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 @@ -67,7 +67,11 @@ public function __construct( public function getInstance(): HandlerInterface { return new \Monolog\Handler\RotatingFileHandler( - sprintf('%s/log/%s', $this->directoryList->getPath('var'), $this->scopeConfig->getValue($this->filenamePath)), + sprintf( + '%s/log/%s', + $this->directoryList->getPath(DirectoryList::VAR_DIR), + $this->scopeConfig->getValue($this->filenamePath) + ), $this->scopeConfig->getValue($this->maxFilesPath), $this->scopeConfig->getValue($this->levelPath) ); diff --git a/Transport/UdpTransportWrapper.php b/Transport/UdpTransportWrapper.php index 9a70040..6585e3c 100755 --- a/Transport/UdpTransportWrapper.php +++ b/Transport/UdpTransportWrapper.php @@ -8,11 +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 function in_array; /** * Class UdpTransportWrapper @@ -21,11 +25,21 @@ */ class UdpTransportWrapper implements TransportInterface { + /** + * @var UdpTransportFactory + */ + private $transportFactory; + /** * @var ScopeConfigInterface */ private $scopeConfig; + /** + * @var LoggerInterface + */ + private $logger; + /** * @var string */ @@ -42,36 +56,41 @@ class UdpTransportWrapper implements TransportInterface private $chunkSize; /** - * @var UdpTransport + * @var string[] */ - private $transporter; + private $ignoredMessages; /** - * @var UdpTransportFactory + * @var UdpTransport */ - private $transportFactory; + private $transporter; - /** - * UdpTransportWrapper constructor. - * - * @param UdpTransportFactory $transportFactory - * @param ScopeConfigInterface $scopeConfig - * @param string $hostPath - * @param string $portPath - * @param string $chunkSize - */ public function __construct( UdpTransportFactory $transportFactory, ScopeConfigInterface $scopeConfig, + LoggerInterface $logger, string $hostPath, string $portPath, - string $chunkSize = UdpTransport::CHUNK_SIZE_LAN + 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,27 +102,23 @@ public function __construct( */ public function send(Message $message): int { - try { - return $this->getTransporter()->send($message); - } catch (\Exception $e) { - 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 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 + ]); } }