Skip to content

Commit

Permalink
Merge pull request #2 from MeCapron/master
Browse files Browse the repository at this point in the history
Handled custom configuration for logger
  • Loading branch information
TonySma committed May 20, 2020
2 parents 6ed764c + 2fa0166 commit 2e6844e
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 3 deletions.
40 changes: 40 additions & 0 deletions Block/Adminhtml/System/Config/Form/Field/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Opengento\Logger\Block\Adminhtml\System\Config\Form\Field;

class Type extends \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
{

/**
* @var \Magento\Framework\Data\Form\Element\Factory
*/
protected $_elementFactory;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\Data\Form\Element\Factory $elementFactory
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Data\Form\Element\Factory $elementFactory,
array $data = []
) {
$this->_elementFactory = $elementFactory;
parent::__construct($context, $data);
}

/**
* Initialise form fields
*
* @return void
*/
protected function _construct()
{
$this->addColumn('custom_logger_key', ['label' => __('Logger key')]);
$this->addColumn('custom_logger_value', ['label' => __('Logger value')]);
$this->_addAfter = false;
$this->_addButtonLabel = __('Add');
parent::_construct();
}
}
15 changes: 15 additions & 0 deletions Config/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Opengento\Logger\Config;

use \Magento\Framework\App\Helper\AbstractHelper;

/**
* Class Config
* @package Opengento\Logger\Config
*/
class Config extends AbstractHelper
{
/** Config keys */
const CONFIG_LOGGER_CUSTOM_CONFIGURATION = 'loggin/loggin/types_logger';
}
57 changes: 57 additions & 0 deletions Config/CustomConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Opengento\Logger\Config;

/**
* Class CustomConfiguration
*/
class CustomConfiguration
{
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var \Magento\Framework\Serialize\Serializer\Json
*/
private $serializer;


/**
* CustomFields constructor.
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Framework\Serialize\Serializer\Json $serializer
*/
public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Serialize\Serializer\Json $serializer)
{
$this->scopeConfig = $scopeConfig;
$this->serializer = $serializer;
}

/**
* @param $configPath
* @param null $store
* @return mixed
*/
public function getConfigValue($configPath, $store = null){
return $this->scopeConfig->getValue(
$configPath,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$store
);
}

/**
* @param $configPath
* @param null $store
* @return array|bool|float|int|mixed|string|null
*/
public function getUnserializedConfigValue($configPath, $store = null){
$value = $this->getConfigValue($configPath, $store);

if(!$value) return false;

return $this->serializer->unserialize($value);
}
}
32 changes: 31 additions & 1 deletion Publisher/GelfPublisherWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/

namespace Opengento\Logger\Publisher;

use Gelf\MessageInterface;
use Gelf\PublisherInterface;
use Opengento\Logger\Config\CustomConfiguration;
use Opengento\Logger\Config\Config;

/**
* Class GelfPublisherWrapper
Expand All @@ -20,8 +23,19 @@ class GelfPublisherWrapper implements PublisherInterface
*/
private $publisher;

public function __construct(PublisherInterface $publisher)
/**
* @var CustomConfiguration
*/
private $customConfiguration;

/**
* GelfPublisherWrapper constructor.
* @param PublisherInterface $publisher
* @param CustomConfiguration $customConfiguration
*/
public function __construct(PublisherInterface $publisher, CustomConfiguration $customConfiguration)
{
$this->customConfiguration = $customConfiguration;
$this->publisher = $publisher;
}

Expand All @@ -34,6 +48,22 @@ public function __construct(PublisherInterface $publisher)
*/
public function publish(MessageInterface $message)
{
$this->joinCustomConfiguration($message);

return $this->publisher->publish($message);
}

/**
* @param MessageInterface $message
*/
public function joinCustomConfiguration(MessageInterface $message)
{
$customConfiguration = $this->customConfiguration->getUnserializedConfigValue(Config::CONFIG_LOGGER_CUSTOM_CONFIGURATION);

if(!$customConfiguration) return;

foreach ($customConfiguration as $value) {
$message->setAdditional($value['custom_logger_key'], $value['custom_logger_value']);
}
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"graylog2/gelf-php": "^1.6"
},
"type": "magento2-module",
"version": "0.1.0",
"version": "0.2.0",
"license": [
"MIT"
],
Expand Down
6 changes: 6 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
<label>Gelf transport port</label>
<comment/>
</field>
<field id="types_logger" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Custom configuration</label>
<frontend_model>Opengento\Logger\Block\Adminhtml\System\Config\Form\Field\Type</frontend_model>
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
<comment>Add key with custom value</comment>
</field>
</group>
</section>
</system>
Expand Down
8 changes: 7 additions & 1 deletion etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@
</arguments>
</virtualType>

<virtualType name="SimpleGelfHandler" type="Opengento\Logger\Handler\GelfHandlerWrapper">
<virtualType name="gelfPublisherWrapper" type="Opengento\Logger\Publisher\GelfPublisherWrapper" >
<arguments>
<argument name="publisher" xsi:type="object">gelfPublisher</argument>
</arguments>
</virtualType>

<virtualType name="SimpleGelfHandler" type="Opengento\Logger\Handler\GelfHandlerWrapper">
<arguments>
<argument name="publisher" xsi:type="object">gelfPublisherWrapper</argument>
<argument name="level" xsi:type="init_parameter">Monolog\Logger::INFO</argument>
</arguments>
</virtualType>
Expand Down
7 changes: 7 additions & 0 deletions i18n/fr_FR.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"Enable ?", "Activé ?"
"Gelf transport host", "Gelf transport host"
"Gelf transport port", "Gelf transport port"
"Custom configuration", "Configuration personnalisée"
"Add key with custom value", "Ajoute une clé avec des valeurs personnalisées"
"Logger key", "Logger key"
"Logger value", "Logger value"

0 comments on commit 2e6844e

Please sign in to comment.