From a65589ef7e78cf5aac5d0a2aca99f9a0ec423929 Mon Sep 17 00:00:00 2001 From: Thomas Klein Date: Thu, 11 Aug 2022 22:49:09 +0200 Subject: [PATCH] Code cleanup --- .../System/Config/Form/Field/Type.php | 3 +- Config/Config.php | 17 --- Config/CustomConfiguration.php | 48 --------- Handler/ConsoleHandler.php | 7 +- Handler/GelfHandler.php | 2 +- Handler/MailHandler.php | 2 +- Handler/RotatingFileHandler.php | 4 +- Handler/SlackHandler.php | 2 +- Plugin/MonologPlugin.php | 5 +- Processor/CustomContextProcessor.php | 34 +++--- Transport/UdpTransportWrapper.php | 2 +- etc/adminhtml/system.xml | 101 ++++++------------ registration.php | 8 +- 13 files changed, 68 insertions(+), 167 deletions(-) delete mode 100644 Config/Config.php delete mode 100644 Config/CustomConfiguration.php diff --git a/Block/Adminhtml/System/Config/Form/Field/Type.php b/Block/Adminhtml/System/Config/Form/Field/Type.php index 2b32244..2ffb0d7 100644 --- a/Block/Adminhtml/System/Config/Form/Field/Type.php +++ b/Block/Adminhtml/System/Config/Form/Field/Type.php @@ -12,12 +12,11 @@ class Type extends AbstractFieldArray { - protected function _construct() + protected function _prepareToRender(): void { $this->addColumn('custom_logger_key', ['label' => __('Logger key')]); $this->addColumn('custom_logger_value', ['label' => __('Logger value')]); $this->_addAfter = false; $this->_addButtonLabel = __('Add'); - parent::_construct(); } } diff --git a/Config/Config.php b/Config/Config.php deleted file mode 100644 index d3c9185..0000000 --- a/Config/Config.php +++ /dev/null @@ -1,17 +0,0 @@ -scopeConfig = $scopeConfig; - $this->serializer = $serializer; - } - - public function getUnserializedConfigValue(string $configPath, ?string $store = null): ?array - { - $value = $this->getConfigValue($configPath, $store); - - if (!$value) { - return null; - } - - return $this->serializer->unserialize($value); - } - - public function getConfigValue(string $configPath, ?string $store = null) - { - return $this->scopeConfig->getValue($configPath, ScopeInterface::SCOPE_STORE, $store); - } -} diff --git a/Handler/ConsoleHandler.php b/Handler/ConsoleHandler.php index 52300ae..c809e11 100644 --- a/Handler/ConsoleHandler.php +++ b/Handler/ConsoleHandler.php @@ -45,14 +45,11 @@ public function __construct( */ public function getInstance(): HandlerInterface { - return new StreamHandler( - 'php://stdout', - $this->scopeConfig->getValue($this->levelPath) - ); + return new StreamHandler('php://stdout', $this->scopeConfig->getValue($this->levelPath)); } public function isEnabled(): bool { - return (bool)$this->scopeConfig->getValue($this->isEnabled); + return $this->scopeConfig->isSetFlag($this->isEnabled); } } diff --git a/Handler/GelfHandler.php b/Handler/GelfHandler.php index 1792fd6..8904b21 100644 --- a/Handler/GelfHandler.php +++ b/Handler/GelfHandler.php @@ -57,6 +57,6 @@ public function getInstance(): HandlerInterface public function isEnabled(): bool { - return (bool)$this->scopeConfig->getValue($this->isEnabled); + return $this->scopeConfig->isSetFlag($this->isEnabled); } } diff --git a/Handler/MailHandler.php b/Handler/MailHandler.php index 704cea4..6d10722 100644 --- a/Handler/MailHandler.php +++ b/Handler/MailHandler.php @@ -72,6 +72,6 @@ public function getInstance(): HandlerInterface public function isEnabled(): bool { - return (bool)$this->scopeConfig->getValue($this->isEnabled); + return $this->scopeConfig->isSetFlag($this->isEnabled); } } diff --git a/Handler/RotatingFileHandler.php b/Handler/RotatingFileHandler.php index dc6bbd5..7a8ad8e 100644 --- a/Handler/RotatingFileHandler.php +++ b/Handler/RotatingFileHandler.php @@ -79,6 +79,6 @@ public function getInstance(): HandlerInterface public function isEnabled(): bool { - return (bool)$this->scopeConfig->getValue($this->isEnabled); + return $this->scopeConfig->isSetFlag($this->isEnabled); } -} \ No newline at end of file +} diff --git a/Handler/SlackHandler.php b/Handler/SlackHandler.php index 2ca893b..871ba03 100644 --- a/Handler/SlackHandler.php +++ b/Handler/SlackHandler.php @@ -116,6 +116,6 @@ public function getInstance(): HandlerInterface public function isEnabled(): bool { - return (bool)$this->scopeConfig->getValue($this->isEnabled); + return $this->scopeConfig->isSetFlag($this->isEnabled); } } diff --git a/Plugin/MonologPlugin.php b/Plugin/MonologPlugin.php index 460f23e..526eb9d 100644 --- a/Plugin/MonologPlugin.php +++ b/Plugin/MonologPlugin.php @@ -4,6 +4,8 @@ * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Opengento\Logger\Plugin; use Magento\Framework\Logger\Monolog; @@ -18,8 +20,7 @@ public function aroundSetHandlers(Monolog $subject, callable $proceed, array $ha foreach ($handlers as $handler) { if ($handler instanceof MagentoHandlerInterface && $handler->isEnabled()) { $magentoHandlers[] = $handler->getInstance(); - } - if ($handler instanceof HandlerInterface) { + } elseif ($handler instanceof HandlerInterface) { $magentoHandlers[] = $handler; } } diff --git a/Processor/CustomContextProcessor.php b/Processor/CustomContextProcessor.php index 54e9d75..6b8af3a 100755 --- a/Processor/CustomContextProcessor.php +++ b/Processor/CustomContextProcessor.php @@ -8,34 +8,42 @@ namespace Opengento\Logger\Processor; +use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Framework\Serialize\Serializer\Json; +use Magento\Store\Model\ScopeInterface; use Monolog\Processor\ProcessorInterface; -use Opengento\Logger\Config\Config; -use Opengento\Logger\Config\CustomConfiguration; class CustomContextProcessor implements ProcessorInterface { /** - * @var CustomConfiguration + * @var ScopeConfigInterface */ - private $customConfiguration; + private $scopeConfig; - public function __construct(CustomConfiguration $customConfiguration) + /** + * @var Json + */ + private $serializer; + + public function __construct(ScopeConfigInterface $scopeConfig, Json $serializer) { - $this->customConfiguration = $customConfiguration; + $this->scopeConfig = $scopeConfig; + $this->serializer = $serializer; } public function __invoke(array $records): array { - $customConfiguration = $this->customConfiguration->getUnserializedConfigValue(Config::CONFIG_LOGGER_CUSTOM_CONFIGURATION); - - if (!$customConfiguration) { - return $records; - } - - foreach ($customConfiguration as $value) { + foreach ($this->resolveTypesLogger() as $value) { $records['context'][$value['custom_logger_key']] = $value['custom_logger_value']; } return $records; } + + private function resolveTypesLogger(?string $store = null): array + { + return $this->serializer->unserialize( + $this->scopeConfig->getValue('loggin/context/types_logger', ScopeInterface::SCOPE_STORE, $store) ?: '{}' + ); + } } diff --git a/Transport/UdpTransportWrapper.php b/Transport/UdpTransportWrapper.php index 6585e3c..2c1a2ad 100755 --- a/Transport/UdpTransportWrapper.php +++ b/Transport/UdpTransportWrapper.php @@ -115,7 +115,7 @@ public function send(Message $message): int private function createTransporter(): UdpTransport { - return $this->transporter = $this->transportFactory->create([ + return $this->transportFactory->create([ 'host' => $this->scopeConfig->getValue($this->hostPath), 'port' => $this->scopeConfig->getValue($this->portPath), 'chunkSize' => $this->chunkSize diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 5f61402..e53cd44 100755 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -5,8 +5,7 @@ * See LICENSE bundled with this library for license details. */ --> - + @@ -17,8 +16,7 @@ Opengento_Logger::config_opengento_logger - + Opengento\Logger\Block\Adminhtml\System\Config\Form\Field\Type Magento\Config\Model\Config\Backend\Serialized\ArraySerialized @@ -27,23 +25,17 @@ - - - + + Magento\Config\Model\Config\Source\Yesno - + - - + - - Opengento\Logger\Config\Source\LoggingLevel @@ -52,44 +44,34 @@ - - - + + Magento\Config\Model\Config\Source\Yesno - + Opengento\Logger\Config\Source\LoggingLevel - Messages of this or a higher level will be logged in console. - - - + + Magento\Config\Model\Config\Source\Yesno - + To who you want to send the mail, you can specify multiple mails by separating them by a coma (,) - + The subject of the mail - + From who you want to send the mail - + Opengento\Logger\Config\Source\LoggingLevel Messages of this or a higher level will be logged in mail. @@ -97,25 +79,20 @@ - - - + + Magento\Config\Model\Config\Source\Yesno - + Opengento\Logger\Config\Source\LoggingLevel Messages of this or a higher level will be logged in files. - + Name of file where the logs must be sent (it will be sent in var/log folder) - + Messages of this or a higher level will be logged in files. validate-digits validate-zero-or-greater @@ -123,61 +100,47 @@ - - - + + Magento\Config\Model\Config\Source\Yesno - + Opengento\Logger\Config\Source\LoggingLevel Messages of this or a higher level will be logged in files. - + Slack API token - + Slack channel (encoded ID or name) - + Name of a bot - + Whether the message should be added to Slack as attachment (plain text otherwise) Magento\Config\Model\Config\Source\Yesno - + The emoji name to use (or null) - + Whether the messages that are handled can bubble up the stack or not Magento\Config\Model\Config\Source\Yesno - + Whether the the context/extra messages added to Slack as attachments are in a short style Magento\Config\Model\Config\Source\Yesno - + Whether the attachment should include context and extra data Magento\Config\Model\Config\Source\Yesno diff --git a/registration.php b/registration.php index fcd74fc..55227b4 100755 --- a/registration.php +++ b/registration.php @@ -4,10 +4,8 @@ * See LICENSE bundled with this library for license details. */ +declare(strict_types=1); + use Magento\Framework\Component\ComponentRegistrar; -ComponentRegistrar::register( - ComponentRegistrar::MODULE, - 'Opengento_Logger', - __DIR__ -); +ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Opengento_Logger', __DIR__);