Skip to content

Commit 0bd6635

Browse files
authored
Merge pull request #19 from MeCapron/feature/enhance_elasticsearch_handler
Feature/enhance elasticsearch handler
2 parents 4f965a9 + 6e4d782 commit 0bd6635

File tree

5 files changed

+63
-13
lines changed

5 files changed

+63
-13
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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\Client\Elasticsearch;
10+
11+
use Elastic\Elasticsearch\ClientBuilder as ClientBuilder8;
12+
use Elasticsearch\ClientBuilder as ClientBuilder7;
13+
use RuntimeException;
14+
15+
class ElasticsearchClientFactory
16+
{
17+
public static function create(): ClientBuilder7|ClientBuilder8
18+
{
19+
if (class_exists(ClientBuilder8::class)) {
20+
return ClientBuilder8::create();
21+
}
22+
23+
if (class_exists(ClientBuilder7::class)) {
24+
return ClientBuilder7::create();
25+
}
26+
27+
throw new RuntimeException('No compatible Elasticsearch ClientBuilder found.');
28+
}
29+
}

Handler/ElasticsearchHandler.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
namespace Opengento\Logger\Handler;
1010

11-
use Elasticsearch\ClientBuilder;
1211
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Monolog\Handler\BufferHandler;
1313
use Monolog\Handler\ElasticsearchHandler as MonologElasticsearchHandler;
1414
use Monolog\Handler\HandlerInterface;
1515
use Monolog\Handler\NoopHandler;
16+
use Opengento\Logger\Client\Elasticsearch\ElasticsearchClientFactory;
1617
use RuntimeException;
1718

1819
class ElasticsearchHandler implements MagentoHandlerInterface
@@ -33,9 +34,7 @@ public function __construct(
3334

3435
public function getInstance(): HandlerInterface
3536
{
36-
$client = ClientBuilder::create()->setHosts(
37-
[$this->scopeConfig->getValue($this->hostPath)]
38-
);
37+
$client = ElasticsearchClientFactory::create()->setHosts([$this->scopeConfig->getValue($this->hostPath)]);
3938

4039
if ($this->scopeConfig->isSetFlag($this->isAuthenticationEnabledPath)) {
4140
if (
@@ -55,13 +54,19 @@ public function getInstance(): HandlerInterface
5554
);
5655
}
5756

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)
57+
$logLevel = $this->scopeConfig->getValue($this->levelPath);
58+
59+
return new BufferHandler(
60+
new MonologElasticsearchHandler(
61+
$client->build(),
62+
[
63+
'index' => $this->scopeConfig->getValue($this->indexPath),
64+
'ignore_error' => $this->scopeConfig->isSetFlag($this->ignoreErrorPath)
65+
],
66+
$logLevel
67+
),
68+
0,
69+
$logLevel
6570
);
6671
}
6772

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"php": "^8.1",
1515
"magento/framework": ">=100.1.0",
1616
"graylog2/gelf-php": "^1.6.0 || ^2.0.0",
17-
"elasticsearch/elasticsearch": ">=7.17.2"
17+
"elasticsearch/elasticsearch": "^7.17.2 || ^8.0.0"
1818
},
1919
"require-dev": {
2020
"magento/magento-coding-standard": "^5",

etc/adminhtml/system.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,23 @@
5151
<field id="host" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label" type="text">
5252
<label>Elasticsearch / Opensearch transport host, only one supported</label>
5353
<comment>Please specify the port like : https://my-elastic-host:443 otherwise it will fallback to 9200.</comment>
54+
<depends>
55+
<field id="is_enabled">1</field>
56+
</depends>
5457
</field>
5558
<field id="index" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="15" translate="label" type="text">
5659
<label>Elasticsearch / Opensearch index</label>
5760
<comment>Index where the logs will be stored</comment>
61+
<depends>
62+
<field id="is_enabled">1</field>
63+
</depends>
5864
</field>
5965
<field id="is_authentication_enabled" showInDefault="1" showInStore="0" showInWebsite="0" sortOrder="20" translate="label" type="select">
6066
<label>Authentication enabled</label>
6167
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
68+
<depends>
69+
<field id="is_enabled">1</field>
70+
</depends>
6271
</field>
6372
<field id="username" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="25" translate="label" type="text">
6473
<label>Username</label>
@@ -70,6 +79,7 @@
7079
<field id="password" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label" type="obscure">
7180
<label>Password</label>
7281
<comment>Only used when authentication is enabled</comment>
82+
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
7383
<depends>
7484
<field id="is_authentication_enabled">1</field>
7585
</depends>
@@ -79,11 +89,17 @@
7989
<label>Level</label>
8090
<source_model>Opengento\Logger\Config\Source\LoggingLevel</source_model>
8191
<comment>Messages of this or a higher level will be logged in your log system.</comment>
92+
<depends>
93+
<field id="is_enabled">1</field>
94+
</depends>
8295
</field>
8396
<field id="ignore_error" showInDefault="1" showInStore="0" showInWebsite="0" sortOrder="40" translate="label" type="select">
8497
<label>Ignore errors</label>
8598
<comment>By default as true because it can block some functionality if connection is badly configured.</comment>
8699
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
100+
<depends>
101+
<field id="is_enabled">1</field>
102+
</depends>
87103
</field>
88104
</group>
89105
<group id="console" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">

etc/config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</mail>
2727
<elasticsearch>
2828
<is_enabled>0</is_enabled>
29-
<level>500</level>
29+
<level>100</level>
3030
<host/>
3131
<index/>
3232
<is_authentication_enabled>0</is_authentication_enabled>

0 commit comments

Comments
 (0)