mirror of
https://github.com/Seldaek/monolog.git
synced 2025-10-24 10:06:08 +02:00
ElasticSearch v8 support (#1662)
* ElasticSearch v8 support * CI updates Co-authored-by: Thomas Müller <mimmi20@live.de>
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
|
||||
namespace Monolog\Handler;
|
||||
|
||||
use Elastic\Elasticsearch\Response\Elasticsearch;
|
||||
use Throwable;
|
||||
use RuntimeException;
|
||||
use Monolog\Logger;
|
||||
@@ -19,6 +20,8 @@ use Monolog\Formatter\ElasticsearchFormatter;
|
||||
use InvalidArgumentException;
|
||||
use Elasticsearch\Common\Exceptions\RuntimeException as ElasticsearchRuntimeException;
|
||||
use Elasticsearch\Client;
|
||||
use Elastic\Elasticsearch\Exception\InvalidArgumentException as ElasticInvalidArgumentException;
|
||||
use Elastic\Elasticsearch\Client as Client8;
|
||||
|
||||
/**
|
||||
* Elasticsearch handler
|
||||
@@ -44,7 +47,7 @@ use Elasticsearch\Client;
|
||||
class ElasticsearchHandler extends AbstractProcessingHandler
|
||||
{
|
||||
/**
|
||||
* @var Client
|
||||
* @var Client|Client8
|
||||
*/
|
||||
protected $client;
|
||||
|
||||
@@ -54,11 +57,20 @@ class ElasticsearchHandler extends AbstractProcessingHandler
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* @param Client $client Elasticsearch Client object
|
||||
* @param mixed[] $options Handler configuration
|
||||
* @var bool
|
||||
*/
|
||||
public function __construct(Client $client, array $options = [], $level = Logger::DEBUG, bool $bubble = true)
|
||||
private $needsType;
|
||||
|
||||
/**
|
||||
* @param Client|Client8 $client Elasticsearch Client object
|
||||
* @param mixed[] $options Handler configuration
|
||||
*/
|
||||
public function __construct($client, array $options = [], $level = Logger::DEBUG, bool $bubble = true)
|
||||
{
|
||||
if (!$client instanceof Client && !$client instanceof Client8) {
|
||||
throw new \TypeError('Elasticsearch\Client or Elastic\Elasticsearch\Client instance required');
|
||||
}
|
||||
|
||||
parent::__construct($level, $bubble);
|
||||
$this->client = $client;
|
||||
$this->options = array_merge(
|
||||
@@ -69,6 +81,14 @@ class ElasticsearchHandler extends AbstractProcessingHandler
|
||||
],
|
||||
$options
|
||||
);
|
||||
|
||||
if ($client instanceof Client8 || $client::VERSION[0] === '7') {
|
||||
$this->needsType = false;
|
||||
// force the type to _doc for ES8/ES7
|
||||
$this->options['type'] = '_doc';
|
||||
} else {
|
||||
$this->needsType = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -133,9 +153,11 @@ class ElasticsearchHandler extends AbstractProcessingHandler
|
||||
|
||||
foreach ($records as $record) {
|
||||
$params['body'][] = [
|
||||
'index' => [
|
||||
'index' => $this->needsType ? [
|
||||
'_index' => $record['_index'],
|
||||
'_type' => $record['_type'],
|
||||
] : [
|
||||
'_index' => $record['_index'],
|
||||
],
|
||||
];
|
||||
unset($record['_index'], $record['_type']);
|
||||
@@ -143,6 +165,7 @@ class ElasticsearchHandler extends AbstractProcessingHandler
|
||||
$params['body'][] = $record;
|
||||
}
|
||||
|
||||
/** @var Elasticsearch */
|
||||
$responses = $this->client->bulk($params);
|
||||
|
||||
if ($responses['errors'] === true) {
|
||||
@@ -160,9 +183,9 @@ class ElasticsearchHandler extends AbstractProcessingHandler
|
||||
*
|
||||
* Only the first error is converted into an exception.
|
||||
*
|
||||
* @param mixed[] $responses returned by $this->client->bulk()
|
||||
* @param mixed[]|Elasticsearch $responses returned by $this->client->bulk()
|
||||
*/
|
||||
protected function createExceptionFromResponses(array $responses): ElasticsearchRuntimeException
|
||||
protected function createExceptionFromResponses($responses): Throwable
|
||||
{
|
||||
foreach ($responses['items'] ?? [] as $item) {
|
||||
if (isset($item['index']['error'])) {
|
||||
@@ -170,6 +193,10 @@ class ElasticsearchHandler extends AbstractProcessingHandler
|
||||
}
|
||||
}
|
||||
|
||||
if (class_exists(ElasticInvalidArgumentException::class)) {
|
||||
return new ElasticInvalidArgumentException('Elasticsearch failed to index one or more records.');
|
||||
}
|
||||
|
||||
return new ElasticsearchRuntimeException('Elasticsearch failed to index one or more records.');
|
||||
}
|
||||
|
||||
@@ -178,10 +205,14 @@ class ElasticsearchHandler extends AbstractProcessingHandler
|
||||
*
|
||||
* @param mixed[] $error
|
||||
*/
|
||||
protected function createExceptionFromError(array $error): ElasticsearchRuntimeException
|
||||
protected function createExceptionFromError(array $error): Throwable
|
||||
{
|
||||
$previous = isset($error['caused_by']) ? $this->createExceptionFromError($error['caused_by']) : null;
|
||||
|
||||
if (class_exists(ElasticInvalidArgumentException::class)) {
|
||||
return new ElasticInvalidArgumentException($error['type'] . ': ' . $error['reason'], 0, $previous);
|
||||
}
|
||||
|
||||
return new ElasticsearchRuntimeException($error['type'] . ': ' . $error['reason'], 0, $previous);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,15 @@ use Monolog\Formatter\FormatterInterface;
|
||||
*/
|
||||
class TestCase extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
if (isset($this->handler)) {
|
||||
unset($this->handler);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $context
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user