Skip to content

Commit ae558b6

Browse files
authored
Merge pull request #17 from MeCapron/feature/add_elasticsearch_handler_api
[Handler] Adding ElasticsearchHandler Monolog
2 parents d52185a + edcab5e commit ae558b6

File tree

7 files changed

+152
-3
lines changed

7 files changed

+152
-3
lines changed

Handler/ElasticsearchHandler.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Opengento\Logger\Handler;
10+
11+
use Elasticsearch\ClientBuilder;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Monolog\Handler\ElasticsearchHandler as MonologElasticsearchHandler;
14+
use Monolog\Handler\HandlerInterface;
15+
use Monolog\Handler\NoopHandler;
16+
use RuntimeException;
17+
18+
readonly class ElasticsearchHandler implements MagentoHandlerInterface
19+
{
20+
public function __construct(
21+
private ScopeConfigInterface $scopeConfig,
22+
private string $isEnabled,
23+
private string $levelPath,
24+
private string $hostPath,
25+
private string $indexPath,
26+
private string $isAuthenticationEnabledPath,
27+
private string $usernamePath,
28+
private string $passwordPath,
29+
private string $ignoreErrorPath,
30+
)
31+
{
32+
}
33+
34+
public function getInstance(): HandlerInterface
35+
{
36+
$client = ClientBuilder::create()->setHosts(
37+
[$this->scopeConfig->getValue($this->hostPath)]
38+
);
39+
40+
if ($this->scopeConfig->isSetFlag($this->isAuthenticationEnabledPath)) {
41+
if (
42+
$this->scopeConfig->getValue($this->usernamePath) === null ||
43+
$this->scopeConfig->getValue($this->passwordPath) === null
44+
) {
45+
if ($this->scopeConfig->isSetFlag($this->ignoreErrorPath)) {
46+
return new NoopHandler();
47+
}
48+
49+
throw new RuntimeException('Elasticsearch authentication is enabled but no credentials are provided.');
50+
}
51+
52+
$client = $client->setBasicAuthentication(
53+
$this->scopeConfig->getValue($this->usernamePath),
54+
$this->scopeConfig->getValue($this->passwordPath)
55+
);
56+
}
57+
58+
return new MonologElasticsearchHandler(
59+
$client->build(),
60+
[
61+
'index' => $this->scopeConfig->getValue($this->indexPath),
62+
'ignore_error' => $this->scopeConfig->isSetFlag($this->ignoreErrorPath)
63+
],
64+
$this->scopeConfig->getValue($this->levelPath)
65+
);
66+
}
67+
68+
public function isEnabled(): bool
69+
{
70+
return $this->scopeConfig->isSetFlag($this->isEnabled);
71+
}
72+
}

Processor/CustomContextProcessor.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ public function __construct(ScopeConfigInterface $scopeConfig, Json $serializer)
3232
$this->serializer = $serializer;
3333
}
3434

35-
public function __invoke(LogRecord $records): LogRecord
35+
/**
36+
* LogRecord is used for Monolog 3.x (Magento >= 2.4.8)
37+
* Array is used for Monolog 2.x (Magento <= 2.4.7)
38+
*/
39+
public function __invoke(LogRecord|array $records): LogRecord|array
3640
{
3741
foreach ($this->resolveTypesLogger() as $value) {
3842
$records['context'][$value['custom_logger_key']] = $value['custom_logger_value'];

Processor/ExceptionProcessor.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313

1414
class ExceptionProcessor implements ProcessorInterface
1515
{
16-
public function __invoke(LogRecord $records): LogRecord
16+
/**
17+
* LogRecord is used for Monolog 3.x (Magento >= 2.4.8)
18+
* Array is used for Monolog 2.x (Magento <= 2.4.7)
19+
*/
20+
public function __invoke(LogRecord|array $records): LogRecord|array
1721
{
1822
if (isset($records['context']['exception'])) {
1923
$records['extra']['stacktrace'] = $records['context']['exception']->getTraceAsString();

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"require": {
1414
"php": "^7.1||^8.0",
1515
"magento/framework": ">=100.1.0",
16-
"graylog2/gelf-php": "^1.6.0 || ^2.0.0"
16+
"graylog2/gelf-php": "^1.6.0 || ^2.0.0",
17+
"elasticsearch/elasticsearch": ">=7.17.2"
1718
},
1819
"require-dev": {
1920
"magento/magento-coding-standard": "^5",

etc/adminhtml/system.xml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,50 @@
4242
<comment>Messages of this or a higher level will be logged in your log system.</comment>
4343
</field>
4444
</group>
45+
<group id="elasticsearch" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
46+
<label>Elasticsearch / Opensearch</label>
47+
<field id="is_enabled" showInDefault="1" showInStore="0" showInWebsite="0" sortOrder="5" translate="label" type="select">
48+
<label>Enabled</label>
49+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
50+
</field>
51+
<field id="host" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label" type="text">
52+
<label>Elasticsearch / Opensearch transport host, only one supported</label>
53+
<comment>Please specify the port like : https://my-elastic-host:443 otherwise it will fallback to 9200.</comment>
54+
</field>
55+
<field id="index" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="15" translate="label" type="text">
56+
<label>Elasticsearch / Opensearch index</label>
57+
<comment>Index where the logs will be stored</comment>
58+
</field>
59+
<field id="is_authentication_enabled" showInDefault="1" showInStore="0" showInWebsite="0" sortOrder="20" translate="label" type="select">
60+
<label>Authentication enabled</label>
61+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
62+
</field>
63+
<field id="username" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="25" translate="label" type="text">
64+
<label>Username</label>
65+
<comment>Only used when authentication is enabled</comment>
66+
<depends>
67+
<field id="is_authentication_enabled">1</field>
68+
</depends>
69+
</field>
70+
<field id="password" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label" type="obscure">
71+
<label>Password</label>
72+
<comment>Only used when authentication is enabled</comment>
73+
<depends>
74+
<field id="is_authentication_enabled">1</field>
75+
</depends>
76+
</field>
77+
<field id="level" showInDefault="1" showInStore="0" showInWebsite="0" sortOrder="35" translate="label comment"
78+
type="select">
79+
<label>Level</label>
80+
<source_model>Opengento\Logger\Config\Source\LoggingLevel</source_model>
81+
<comment>Messages of this or a higher level will be logged in your log system.</comment>
82+
</field>
83+
<field id="ignore_error" showInDefault="1" showInStore="0" showInWebsite="0" sortOrder="40" translate="label" type="select">
84+
<label>Ignore errors</label>
85+
<comment>By default as true because it can block some functionality if connection is badly configured.</comment>
86+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
87+
</field>
88+
</group>
4589
<group id="console" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
4690
<label>Console</label>
4791
<field id="is_enabled" showInDefault="1" showInStore="0" showInWebsite="0" sortOrder="5" translate="label" type="select">

etc/config.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@
2424
<level>500</level>
2525
<subject>Opengento Logger is talking</subject>
2626
</mail>
27+
<elasticsearch>
28+
<is_enabled>0</is_enabled>
29+
<level>500</level>
30+
<host/>
31+
<index/>
32+
<is_authentication_enabled>0</is_authentication_enabled>
33+
<username/>
34+
<password/>
35+
<ignore_error>1</ignore_error>
36+
</elasticsearch>
2737
<slack>
2838
<is_enabled>0</is_enabled>
2939
<level>500</level>

etc/di.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@
2828
</arguments>
2929
</virtualType>
3030

31+
<virtualType name="ElasticsearchHandler" type="Opengento\Logger\Handler\ElasticsearchHandler">
32+
<arguments>
33+
<argument name="isEnabled" xsi:type="string">loggin/elasticsearch/is_enabled</argument>
34+
<argument name="levelPath" xsi:type="string">loggin/elasticsearch/level</argument>
35+
<argument name="hostPath" xsi:type="string">loggin/elasticsearch/host</argument>
36+
<argument name="indexPath" xsi:type="string">loggin/elasticsearch/index</argument>
37+
<argument name="isAuthenticationEnabledPath" xsi:type="string">loggin/elasticsearch/is_authentication_enabled</argument>
38+
<argument name="usernamePath" xsi:type="string">loggin/elasticsearch/username</argument>
39+
<argument name="passwordPath" xsi:type="string">loggin/elasticsearch/password</argument>
40+
<argument name="ignoreErrorPath" xsi:type="string">loggin/elasticsearch/ignore_error</argument>
41+
</arguments>
42+
</virtualType>
43+
3144
<virtualType name="MailHandler" type="Opengento\Logger\Handler\MailHandler">
3245
<arguments>
3346
<argument name="isEnabled" xsi:type="string">loggin/mail/is_enabled</argument>
@@ -94,6 +107,7 @@
94107
<arguments>
95108
<argument name="magentoHandlers" xsi:type="array">
96109
<item name="gelf" xsi:type="object">GelfHandler</item>
110+
<item name="elasticsearch" xsi:type="object">ElasticsearchHandler</item>
97111
<item name="mail" xsi:type="object">MailHandler</item>
98112
<item name="console" xsi:type="object">ConsoleHandler</item>
99113
<item name="logs" xsi:type="object">RotatingFileHandler</item>

0 commit comments

Comments
 (0)