Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 74696f1

Browse filesBrowse files
committed
[MonologBridge] Fixed support of elasticsearch 7.+ in ElasticsearchLogstashHandler
1 parent e94f1f6 commit 74696f1
Copy full SHA for 74696f1

File tree

2 files changed

+62
-7
lines changed
Filter options

2 files changed

+62
-7
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Monolog/Handler/ElasticsearchLogstashHandler.php
+19-7Lines 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 $elasticsearchVersion;
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 $elasticsearchVersion = '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->elasticsearchVersion = $elasticsearchVersion;
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->elasticsearchVersion, '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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Monolog/Tests/Handler/ElasticsearchLogstashHandlerTest.php
+43Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.