Skip to content

Navigation Menu

Sign in
Appearance settings

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

[MonologBridge] Fixed support of elasticsearch 7.+ in ElasticsearchLogstashHandler #46684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ class ElasticsearchLogstashHandler extends AbstractHandler
private $index;
private $client;
private $responses;
private $elasticsearchVersion;

/**
* @param string|int $level The minimum logging level at which this handler will be triggered
*/
public function __construct(string $endpoint = 'http://127.0.0.1:9200', string $index = 'monolog', HttpClientInterface $client = null, $level = Logger::DEBUG, bool $bubble = true)
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')
{
if (!interface_exists(HttpClientInterface::class)) {
throw new \LogicException(sprintf('The "%s" handler needs an HTTP client. Try running "composer require symfony/http-client".', __CLASS__));
Expand All @@ -63,6 +64,7 @@ public function __construct(string $endpoint = 'http://127.0.0.1:9200', string $
$this->index = $index;
$this->client = $client ?: HttpClient::create(['timeout' => 1]);
$this->responses = new \SplObjectStorage();
$this->elasticsearchVersion = $elasticsearchVersion;
}

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

if (version_compare($this->elasticsearchVersion, '7', '>=')) {
$headers = json_encode([
'index' => [
'_index' => $this->index,
],
]);
} else {
$headers = json_encode([
'index' => [
'_index' => $this->index,
'_type' => '_doc',
],
]);
}

$body = '';
foreach ($records as $record) {
foreach ($this->processors as $processor) {
$record = $processor($record);
}

$body .= json_encode([
'index' => [
'_index' => $this->index,
'_type' => '_doc',
],
]);
$body .= $headers;
$body .= "\n";
$body .= $formatter->format($record);
$body .= "\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,49 @@ public function testHandle()
$this->assertSame(1, $callCount);
}

public function testHandleWithElasticsearch8()
{
$callCount = 0;
$responseFactory = function ($method, $url, $options) use (&$callCount) {
$body = <<<EOBODY
{"index":{"_index":"log"}}
{"@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}


EOBODY;

// Monolog 1X
if (\defined(LogstashFormatter::class.'::V1')) {
$body = str_replace(',"monolog_level":200', '', $body);
$body = str_replace(',"monolog_level":300', '', $body);
}

$this->assertSame('POST', $method);
$this->assertSame('http://es:9200/_bulk', $url);
$this->assertSame($body, $options['body']);
$this->assertSame('Content-Type: application/json', $options['normalized_headers']['content-type'][0]);
++$callCount;

return new MockResponse();
};

$handler = new ElasticsearchLogstashHandlerWithHardCodedHostname('http://es:9200', 'log', new MockHttpClient($responseFactory), Logger::DEBUG, true, '8.0.0');

$record = [
'message' => 'My info message',
'context' => [],
'level' => Logger::INFO,
'level_name' => Logger::getLevelName(Logger::INFO),
'channel' => 'app',
'datetime' => new \DateTime('2020-01-01T00:00:00+01:00'),
'extra' => [],
];

$handler->handle($record);

$this->assertSame(1, $callCount);
}

public function testBandleBatch()
{
$callCount = 0;
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.