Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e15852

Browse files
committed
[MonologBridge] Fixed support of elasticsearch 7.+ in ElasticsearchLogstashHandler
1 parent e94f1f6 commit 3e15852

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

src/Symfony/Bridge/Monolog/Handler/ElasticsearchLogstashHandler.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ class ElasticsearchLogstashHandler extends AbstractHandler
4848
private $index;
4949
private $client;
5050
private $responses;
51+
private $elasticversion;
5152

5253
/**
5354
* @param string|int $level The minimum logging level at which this handler will be triggered
5455
*/
55-
public function __construct(string $endpoint = 'http://127.0.0.1:9200', string $index = 'monolog', HttpClientInterface $client = null, $level = Logger::DEBUG, bool $bubble = true)
56+
public function __construct(string $endpoint = 'http://127.0.0.1:9200', ?string $index = 'monolog', HttpClientInterface $client = null, $level = Logger::DEBUG, bool $bubble = true, string $elasticversion = '1.0.0')
5657
{
5758
if (!interface_exists(HttpClientInterface::class)) {
5859
throw new \LogicException(sprintf('The "%s" handler needs an HTTP client. Try running "composer require symfony/http-client".', __CLASS__));
@@ -63,6 +64,7 @@ public function __construct(string $endpoint = 'http://127.0.0.1:9200', string $
6364
$this->index = $index;
6465
$this->client = $client ?: HttpClient::create(['timeout' => 1]);
6566
$this->responses = new \SplObjectStorage();
67+
$this->elasticversion = $elasticversion;
6668
}
6769

6870
public function handle(array $record): bool
@@ -100,18 +102,28 @@ private function sendToElasticsearch(array $records)
100102
{
101103
$formatter = $this->getFormatter();
102104

105+
if (version_compare($this->elasticversion, '7', '>=')) {
106+
$headers = json_encode([
107+
'index' => [
108+
'_index' => $this->index,
109+
],
110+
]);
111+
} else {
112+
$headers = json_encode([
113+
'index' => [
114+
'_index' => $this->index,
115+
'_type' => '_doc',
116+
],
117+
]);
118+
}
119+
103120
$body = '';
104121
foreach ($records as $record) {
105122
foreach ($this->processors as $processor) {
106123
$record = $processor($record);
107124
}
108125

109-
$body .= json_encode([
110-
'index' => [
111-
'_index' => $this->index,
112-
'_type' => '_doc',
113-
],
114-
]);
126+
$body .= $headers;
115127
$body .= "\n";
116128
$body .= $formatter->format($record);
117129
$body .= "\n";

src/Symfony/Bridge/Monolog/Tests/Handler/ElasticsearchLogstashHandlerTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,49 @@ public function testHandle()
6464
$this->assertSame(1, $callCount);
6565
}
6666

67+
public function testHandleWithElasticsearch8()
68+
{
69+
$callCount = 0;
70+
$responseFactory = function ($method, $url, $options) use (&$callCount) {
71+
$body = <<<EOBODY
72+
{"index":{"_index":"log"}}
73+
{"@timestamp":"2020-01-01T00:00:00.000000+01:00","@version":1,"host":"my hostname","message":"My info message","type":"application","channel":"app","level":"INFO","monolog_level":200}
74+
75+
76+
EOBODY;
77+
78+
// Monolog 1X
79+
if (\defined(LogstashFormatter::class.'::V1')) {
80+
$body = str_replace(',"monolog_level":200', '', $body);
81+
$body = str_replace(',"monolog_level":300', '', $body);
82+
}
83+
84+
$this->assertSame('POST', $method);
85+
$this->assertSame('http://es:9200/_bulk', $url);
86+
$this->assertSame($body, $options['body']);
87+
$this->assertSame('Content-Type: application/json', $options['normalized_headers']['content-type'][0]);
88+
++$callCount;
89+
90+
return new MockResponse();
91+
};
92+
93+
$handler = new ElasticsearchLogstashHandlerWithHardCodedHostname('http://es:9200', 'log', new MockHttpClient($responseFactory), Logger::DEBUG, true, '8.0.0');
94+
95+
$record = [
96+
'message' => 'My info message',
97+
'context' => [],
98+
'level' => Logger::INFO,
99+
'level_name' => Logger::getLevelName(Logger::INFO),
100+
'channel' => 'app',
101+
'datetime' => new \DateTime('2020-01-01T00:00:00+01:00'),
102+
'extra' => [],
103+
];
104+
105+
$handler->handle($record);
106+
107+
$this->assertSame(1, $callCount);
108+
}
109+
67110
public function testBandleBatch()
68111
{
69112
$callCount = 0;

0 commit comments

Comments
 (0)