1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-07-30 01:40:30 +02:00

Elastica up to 7 support

This commit is contained in:
patrickkusebauch
2020-12-11 10:55:38 +01:00
parent af59ed294e
commit 76639ef02b
5 changed files with 75 additions and 9 deletions

View File

@@ -28,7 +28,7 @@
"phpunit/phpunit": "^8.5",
"predis/predis": "^1.1",
"rollbar/rollbar": "^1.3",
"ruflin/elastica": ">=0.90 <3.0",
"ruflin/elastica": ">=0.90 <7.0.1",
"swiftmailer/swiftmailer": "^5.3|^6.0",
"phpstan/phpstan": "^0.12.59"
},

View File

@@ -58,6 +58,9 @@ class ElasticaFormatter extends NormalizerFormatter
return $this->index;
}
/**
* @deprecated since Elastica 7 type has no effect
*/
public function getType(): string
{
return $this->type;
@@ -72,7 +75,9 @@ class ElasticaFormatter extends NormalizerFormatter
{
$document = new Document();
$document->setData($record);
$document->setType($this->type);
if(method_exists($document, 'setType')) {
$document->setType($this->type);
}
$document->setIndex($this->index);
return $document;

View File

@@ -25,7 +25,7 @@ use Elastica\Exception\ExceptionInterface;
* $client = new \Elastica\Client();
* $options = array(
* 'index' => 'elastic_index_name',
* 'type' => 'elastic_doc_type',
* 'type' => 'elastic_doc_type', Types have been removed in Elastica 7
* );
* $handler = new ElasticaHandler($client, $options);
* $log = new Logger('application');

View File

@@ -55,9 +55,10 @@ class ElasticaFormatterTest extends \PHPUnit\Framework\TestCase
$this->assertInstanceOf('Elastica\Document', $doc);
// Document parameters
$params = $doc->getParams();
$this->assertEquals('my_index', $params['_index']);
$this->assertEquals('doc_type', $params['_type']);
$this->assertEquals('my_index', $doc->getIndex());
if(method_exists($doc, 'getType')) {
$this->assertEquals('doc_type', $doc->getType());
}
// Document data values
$data = $doc->getData();

View File

@@ -156,7 +156,7 @@ class ElasticaHandlerTest extends TestCase
}
/**
* Integration test using localhost Elastic Search server
* Integration test using localhost Elastic Search server version <7
*
* @covers Monolog\Handler\ElasticaHandler::__construct
* @covers Monolog\Handler\ElasticaHandler::handleBatch
@@ -209,6 +209,61 @@ class ElasticaHandlerTest extends TestCase
$client->request("/{$this->options['index']}", Request::DELETE);
}
/**
* Integration test using localhost Elastic Search server version 7+
*
* @covers Monolog\Handler\ElasticaHandler::__construct
* @covers Monolog\Handler\ElasticaHandler::handleBatch
* @covers Monolog\Handler\ElasticaHandler::bulkSend
* @covers Monolog\Handler\ElasticaHandler::getDefaultFormatter
*/
public function testHandleIntegrationNewESVersion()
{
$msg = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['foo' => 7, 'bar', 'class' => new \stdClass],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => [],
'message' => 'log',
];
$expected = $msg;
$expected['datetime'] = $msg['datetime']->format(\DateTime::ISO8601);
$expected['context'] = [
'class' => '[object] (stdClass: {})',
'foo' => 7,
0 => 'bar',
];
$client = new Client();
$handler = new ElasticaHandler($client, $this->options);
try {
$handler->handleBatch([$msg]);
} catch (\RuntimeException $e) {
$this->markTestSkipped("Cannot connect to Elastic Search server on localhost");
}
// check document id from ES server response
$documentId = $this->getCreatedDocId($client->getLastResponse());
$this->assertNotEmpty($documentId, 'No elastic document id received');
// retrieve document source from ES and validate
$document = $this->getDocSourceFromElastic(
$client,
$this->options['index'],
null,
$documentId
);
$this->assertEquals($expected, $document);
// remove test index from ES
$client->request("/{$this->options['index']}", Request::DELETE);
}
/**
* Return last created document id from ES response
* @param Response $response Elastica Response object
@@ -226,13 +281,18 @@ class ElasticaHandlerTest extends TestCase
* Retrieve document by id from Elasticsearch
* @param Client $client Elastica client
* @param string $index
* @param string $type
* @param ?string $type
* @param string $documentId
* @return array
*/
protected function getDocSourceFromElastic(Client $client, $index, $type, $documentId)
{
$resp = $client->request("/{$index}/{$type}/{$documentId}", Request::GET);
if($type === null) {
$path = "/{$index}/{$documentId}";
} else {
$path = "/{$index}/{$type}/{$documentId}";
}
$resp = $client->request($path, Request::GET);
$data = $resp->getData();
if (!empty($data['_source'])) {
return $data['_source'];