1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-03 11:47:38 +02:00

Rename ElasticSearch to Elasticsearch

This commit is contained in:
Avtandil Kikabidze
2018-06-27 11:48:39 +04:00
parent 34e3a48326
commit 4c7795d310
7 changed files with 67 additions and 67 deletions

View File

@@ -1,7 +1,7 @@
### 2.0.0 ### 2.0.0
- Added support for official ElasticSearch client. To avoid name collision, - Added support for official Elasticsearch client. To avoid name collision,
old `ElasticSearchHandler` renamed to `ElasticaHandler` old `ElasticsearchHandler` renamed to `ElasticaHandler`
- The timezone is now set per Logger instance and not statically, either - The timezone is now set per Logger instance and not statically, either
via ->setTimezone or passed in the constructor. Calls to Logger::setTimezone via ->setTimezone or passed in the constructor. Calls to Logger::setTimezone

View File

@@ -37,7 +37,7 @@
"sentry/sentry": "Allow sending log messages to a Sentry server", "sentry/sentry": "Allow sending log messages to a Sentry server",
"doctrine/couchdb": "Allow sending log messages to a CouchDB server", "doctrine/couchdb": "Allow sending log messages to a CouchDB server",
"ruflin/elastica": "Allow sending log messages to an Elastic Search server", "ruflin/elastica": "Allow sending log messages to an Elastic Search server",
"elasticsearch/elasticsearch": "Allow sending log messages to an Elastic Search server via official client", "elasticsearch/elasticsearch": "Allow sending log messages to an Elasticsearch server via official client",
"php-amqplib/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib", "php-amqplib/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib",
"ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)", "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)",
"ext-mongodb": "Allow sending log messages to a MongoDB server (via driver)", "ext-mongodb": "Allow sending log messages to a MongoDB server (via driver)",

View File

@@ -81,7 +81,7 @@
[Mongo](http://pecl.php.net/package/mongo) extension connection. [Mongo](http://pecl.php.net/package/mongo) extension connection.
- [_CouchDBHandler_](../src/Monolog/Handler/CouchDBHandler.php): Logs records to a CouchDB server. - [_CouchDBHandler_](../src/Monolog/Handler/CouchDBHandler.php): Logs records to a CouchDB server.
- [_DoctrineCouchDBHandler_](../src/Monolog/Handler/DoctrineCouchDBHandler.php): Logs records to a CouchDB server via the Doctrine CouchDB ODM. - [_DoctrineCouchDBHandler_](../src/Monolog/Handler/DoctrineCouchDBHandler.php): Logs records to a CouchDB server via the Doctrine CouchDB ODM.
- [_ElasticSearchHandler_](../src/Monolog/Handler/ElasticSearchHandler.php): Logs records to an Elastic Search server. - [_ElasticsearchHandler_](../src/Monolog/Handler/ElasticsearchHandler.php): Logs records to an Elasticsearch server.
- [_DynamoDbHandler_](../src/Monolog/Handler/DynamoDbHandler.php): Logs records to a DynamoDB table with the [AWS SDK](https://github.com/aws/aws-sdk-php). - [_DynamoDbHandler_](../src/Monolog/Handler/DynamoDbHandler.php): Logs records to a DynamoDB table with the [AWS SDK](https://github.com/aws/aws-sdk-php).
### Wrappers / Special Handlers ### Wrappers / Special Handlers

View File

@@ -14,29 +14,29 @@ namespace Monolog\Formatter;
use DateTime; use DateTime;
/** /**
* Format a log message into an ElasticSearch record * Format a log message into an Elasticsearch record
* *
* @author Avtandil Kikabidze <akalongman@gmail.com> * @author Avtandil Kikabidze <akalongman@gmail.com>
*/ */
class ElasticSearchFormatter extends NormalizerFormatter class ElasticsearchFormatter extends NormalizerFormatter
{ {
/** /**
* @var string ElasticSearch index name * @var string Elasticsearch index name
*/ */
protected $index; protected $index;
/** /**
* @var string ElasticSearch record type * @var string Elasticsearch record type
*/ */
protected $type; protected $type;
/** /**
* @param string $index ElasticSearch index name * @param string $index Elasticsearch index name
* @param string $type ElasticSearch record type * @param string $type Elasticsearch record type
*/ */
public function __construct($index, $type) public function __construct($index, $type)
{ {
// ElasticSearch requires an ISO 8601 format date with optional millisecond precision. // Elasticsearch requires an ISO 8601 format date with optional millisecond precision.
parent::__construct(DateTime::ISO8601); parent::__construct(DateTime::ISO8601);
$this->index = $index; $this->index = $index;
@@ -74,7 +74,7 @@ class ElasticSearchFormatter extends NormalizerFormatter
} }
/** /**
* Convert a log message into an ElasticSearch record * Convert a log message into an Elasticsearch record
* *
* @param array $record Log message * @param array $record Log message
* @return array * @return array

View File

@@ -12,36 +12,36 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Elasticsearch\Client; use Elasticsearch\Client;
use Elasticsearch\Common\Exceptions\RuntimeException as ElasticSearchRuntimeException; use Elasticsearch\Common\Exceptions\RuntimeException as ElasticsearchRuntimeException;
use InvalidArgumentException; use InvalidArgumentException;
use Monolog\Formatter\ElasticSearchFormatter; use Monolog\Formatter\ElasticsearchFormatter;
use Monolog\Formatter\FormatterInterface; use Monolog\Formatter\FormatterInterface;
use Monolog\Logger; use Monolog\Logger;
use RuntimeException; use RuntimeException;
use Throwable; use Throwable;
/** /**
* Elastic Search handler * Elasticsearch handler
* *
* @link https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html * @link https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html
* *
* Simple usage example: * Simple usage example:
* *
* $client = \ElasticSearch\ClientBuilder::create() * $client = \Elasticsearch\ClientBuilder::create()
* ->setHosts($hosts) * ->setHosts($hosts)
* ->build(); * ->build();
* *
* $options = array( * $options = array(
* 'index' => 'elastic_index_name', * 'index' => 'elastic_index_name',
* 'type' => 'elastic_doc_type', * 'type' => 'elastic_doc_type',
* ); * );
* $handler = new ElasticSearchHandler($client, $options); * $handler = new ElasticsearchHandler($client, $options);
* $log = new Logger('application'); * $log = new Logger('application');
* $log->pushHandler($handler); * $log->pushHandler($handler);
* *
* @author Avtandil Kikabidze <akalongman@gmail.com> * @author Avtandil Kikabidze <akalongman@gmail.com>
*/ */
class ElasticSearchHandler extends AbstractProcessingHandler class ElasticsearchHandler extends AbstractProcessingHandler
{ {
/** /**
* @var \Elasticsearch\Client * @var \Elasticsearch\Client
@@ -54,7 +54,7 @@ class ElasticSearchHandler extends AbstractProcessingHandler
protected $options = []; protected $options = [];
/** /**
* @param \Elasticsearch\Client $client ElasticSearch Client object * @param \Elasticsearch\Client $client Elasticsearch Client object
* @param array $options Handler configuration * @param array $options Handler configuration
* @param int $level The minimum logging level at which this handler will be triggered * @param int $level The minimum logging level at which this handler will be triggered
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
@@ -67,7 +67,7 @@ class ElasticSearchHandler extends AbstractProcessingHandler
[ [
'index' => 'monolog', // Elastic index name 'index' => 'monolog', // Elastic index name
'type' => '_doc', // Elastic document type 'type' => '_doc', // Elastic document type
'ignore_error' => false, // Suppress ElasticSearch exceptions 'ignore_error' => false, // Suppress Elasticsearch exceptions
], ],
$options $options
); );
@@ -86,10 +86,10 @@ class ElasticSearchHandler extends AbstractProcessingHandler
*/ */
public function setFormatter(FormatterInterface $formatter): HandlerInterface public function setFormatter(FormatterInterface $formatter): HandlerInterface
{ {
if ($formatter instanceof ElasticSearchFormatter) { if ($formatter instanceof ElasticsearchFormatter) {
return parent::setFormatter($formatter); return parent::setFormatter($formatter);
} }
throw new InvalidArgumentException('ElasticSearchHandler is only compatible with ElasticSearchFormatter'); throw new InvalidArgumentException('ElasticsearchHandler is only compatible with ElasticsearchFormatter');
} }
/** /**
@@ -107,7 +107,7 @@ class ElasticSearchHandler extends AbstractProcessingHandler
*/ */
protected function getDefaultFormatter(): FormatterInterface protected function getDefaultFormatter(): FormatterInterface
{ {
return new ElasticSearchFormatter($this->options['index'], $this->options['type']); return new ElasticsearchFormatter($this->options['index'], $this->options['type']);
} }
/** /**
@@ -120,7 +120,7 @@ class ElasticSearchHandler extends AbstractProcessingHandler
} }
/** /**
* Use ElasticSearch bulk API to send list of documents * Use Elasticsearch bulk API to send list of documents
* *
* @param array $records * @param array $records
* @throws \RuntimeException * @throws \RuntimeException
@@ -147,11 +147,11 @@ class ElasticSearchHandler extends AbstractProcessingHandler
$responses = $this->client->bulk($params); $responses = $this->client->bulk($params);
if ($responses['errors'] === true) { if ($responses['errors'] === true) {
throw new ElasticSearchRuntimeException('ElasticSearch returned error for one of the records'); throw new ElasticsearchRuntimeException('Elasticsearch returned error for one of the records');
} }
} catch (Throwable $e) { } catch (Throwable $e) {
if (! $this->options['ignore_error']) { if (! $this->options['ignore_error']) {
throw new RuntimeException('Error sending messages to ElasticSearch', 0, $e); throw new RuntimeException('Error sending messages to Elasticsearch', 0, $e);
} }
} }
} }

View File

@@ -13,12 +13,12 @@ namespace Monolog\Formatter;
use Monolog\Logger; use Monolog\Logger;
class ElasticSearchFormatterTest extends \PHPUnit\Framework\TestCase class ElasticsearchFormatterTest extends \PHPUnit\Framework\TestCase
{ {
/** /**
* @covers Monolog\Formatter\ElasticSearchFormatter::__construct * @covers Monolog\Formatter\ElasticsearchFormatter::__construct
* @covers Monolog\Formatter\ElasticSearchFormatter::format * @covers Monolog\Formatter\ElasticsearchFormatter::format
* @covers Monolog\Formatter\ElasticSearchFormatter::getDocument * @covers Monolog\Formatter\ElasticsearchFormatter::getDocument
*/ */
public function testFormat() public function testFormat()
{ {
@@ -43,7 +43,7 @@ class ElasticSearchFormatterTest extends \PHPUnit\Framework\TestCase
]; ];
// Format log message // Format log message
$formatter = new ElasticSearchFormatter('my_index', 'doc_type'); $formatter = new ElasticsearchFormatter('my_index', 'doc_type');
$doc = $formatter->format($msg); $doc = $formatter->format($msg);
$this->assertInternalType('array', $doc); $this->assertInternalType('array', $doc);
@@ -58,12 +58,12 @@ class ElasticSearchFormatterTest extends \PHPUnit\Framework\TestCase
} }
/** /**
* @covers Monolog\Formatter\ElasticSearchFormatter::getIndex * @covers Monolog\Formatter\ElasticsearchFormatter::getIndex
* @covers Monolog\Formatter\ElasticSearchFormatter::getType * @covers Monolog\Formatter\ElasticsearchFormatter::getType
*/ */
public function testGetters() public function testGetters()
{ {
$formatter = new ElasticSearchFormatter('my_index', 'doc_type'); $formatter = new ElasticsearchFormatter('my_index', 'doc_type');
$this->assertEquals('my_index', $formatter->getIndex()); $this->assertEquals('my_index', $formatter->getIndex());
$this->assertEquals('doc_type', $formatter->getType()); $this->assertEquals('doc_type', $formatter->getType());
} }

View File

@@ -12,13 +12,13 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Elasticsearch\ClientBuilder; use Elasticsearch\ClientBuilder;
use Monolog\Formatter\ElasticSearchFormatter; use Monolog\Formatter\ElasticsearchFormatter;
use Monolog\Formatter\NormalizerFormatter; use Monolog\Formatter\NormalizerFormatter;
use Monolog\Test\TestCase; use Monolog\Test\TestCase;
use Monolog\Logger; use Monolog\Logger;
use Elasticsearch\Client; use Elasticsearch\Client;
class ElasticSearchHandlerTest extends TestCase class ElasticsearchHandlerTest extends TestCase
{ {
/** /**
* @var Client mock * @var Client mock
@@ -35,12 +35,12 @@ class ElasticSearchHandlerTest extends TestCase
public function setUp() public function setUp()
{ {
// ElasticSearch lib required // Elasticsearch lib required
if (!class_exists('Elasticsearch\Client')) { if (!class_exists('Elasticsearch\Client')) {
$this->markTestSkipped('elasticsearch/elasticsearch not installed'); $this->markTestSkipped('elasticsearch/elasticsearch not installed');
} }
// base mock ElasticSearch Client object // base mock Elasticsearch Client object
$this->client = $this->getMockBuilder('Elasticsearch\Client') $this->client = $this->getMockBuilder('Elasticsearch\Client')
->setMethods(['bulk']) ->setMethods(['bulk'])
->disableOriginalConstructor() ->disableOriginalConstructor()
@@ -48,10 +48,10 @@ class ElasticSearchHandlerTest extends TestCase
} }
/** /**
* @covers Monolog\Handler\ElasticSearchHandler::write * @covers Monolog\Handler\ElasticsearchHandler::write
* @covers Monolog\Handler\ElasticSearchHandler::handleBatch * @covers Monolog\Handler\ElasticsearchHandler::handleBatch
* @covers Monolog\Handler\ElasticSearchHandler::bulkSend * @covers Monolog\Handler\ElasticsearchHandler::bulkSend
* @covers Monolog\Handler\ElasticSearchHandler::getDefaultFormatter * @covers Monolog\Handler\ElasticsearchHandler::getDefaultFormatter
*/ */
public function testHandle() public function testHandle()
{ {
@@ -67,7 +67,7 @@ class ElasticSearchHandlerTest extends TestCase
]; ];
// format expected result // format expected result
$formatter = new ElasticSearchFormatter($this->options['index'], $this->options['type']); $formatter = new ElasticsearchFormatter($this->options['index'], $this->options['type']);
$data = $formatter->format($msg); $data = $formatter->format($msg);
unset($data['_index'], $data['_type']); unset($data['_index'], $data['_type']);
@@ -89,39 +89,39 @@ class ElasticSearchHandlerTest extends TestCase
->with($expected); ->with($expected);
// perform tests // perform tests
$handler = new ElasticSearchHandler($this->client, $this->options); $handler = new ElasticsearchHandler($this->client, $this->options);
$handler->handle($msg); $handler->handle($msg);
$handler->handleBatch([$msg]); $handler->handleBatch([$msg]);
} }
/** /**
* @covers Monolog\Handler\ElasticSearchHandler::setFormatter * @covers Monolog\Handler\ElasticsearchHandler::setFormatter
*/ */
public function testSetFormatter() public function testSetFormatter()
{ {
$handler = new ElasticSearchHandler($this->client); $handler = new ElasticsearchHandler($this->client);
$formatter = new ElasticSearchFormatter('index_new', 'type_new'); $formatter = new ElasticsearchFormatter('index_new', 'type_new');
$handler->setFormatter($formatter); $handler->setFormatter($formatter);
$this->assertInstanceOf('Monolog\Formatter\ElasticSearchFormatter', $handler->getFormatter()); $this->assertInstanceOf('Monolog\Formatter\ElasticsearchFormatter', $handler->getFormatter());
$this->assertEquals('index_new', $handler->getFormatter()->getIndex()); $this->assertEquals('index_new', $handler->getFormatter()->getIndex());
$this->assertEquals('type_new', $handler->getFormatter()->getType()); $this->assertEquals('type_new', $handler->getFormatter()->getType());
} }
/** /**
* @covers Monolog\Handler\ElasticSearchHandler::setFormatter * @covers Monolog\Handler\ElasticsearchHandler::setFormatter
* @expectedException InvalidArgumentException * @expectedException InvalidArgumentException
* @expectedExceptionMessage ElasticSearchHandler is only compatible with ElasticSearchFormatter * @expectedExceptionMessage ElasticsearchHandler is only compatible with ElasticsearchFormatter
*/ */
public function testSetFormatterInvalid() public function testSetFormatterInvalid()
{ {
$handler = new ElasticSearchHandler($this->client); $handler = new ElasticsearchHandler($this->client);
$formatter = new NormalizerFormatter(); $formatter = new NormalizerFormatter();
$handler->setFormatter($formatter); $handler->setFormatter($formatter);
} }
/** /**
* @covers Monolog\Handler\ElasticSearchHandler::__construct * @covers Monolog\Handler\ElasticsearchHandler::__construct
* @covers Monolog\Handler\ElasticSearchHandler::getOptions * @covers Monolog\Handler\ElasticsearchHandler::getOptions
*/ */
public function testOptions() public function testOptions()
{ {
@@ -130,12 +130,12 @@ class ElasticSearchHandlerTest extends TestCase
'type' => $this->options['type'], 'type' => $this->options['type'],
'ignore_error' => false, 'ignore_error' => false,
]; ];
$handler = new ElasticSearchHandler($this->client, $this->options); $handler = new ElasticsearchHandler($this->client, $this->options);
$this->assertEquals($expected, $handler->getOptions()); $this->assertEquals($expected, $handler->getOptions());
} }
/** /**
* @covers Monolog\Handler\ElasticSearchHandler::bulkSend * @covers Monolog\Handler\ElasticsearchHandler::bulkSend
* @dataProvider providerTestConnectionErrors * @dataProvider providerTestConnectionErrors
*/ */
public function testConnectionErrors($ignore, $expectedError) public function testConnectionErrors($ignore, $expectedError)
@@ -146,7 +146,7 @@ class ElasticSearchHandlerTest extends TestCase
->build(); ->build();
$handlerOpts = ['ignore_error' => $ignore]; $handlerOpts = ['ignore_error' => $ignore];
$handler = new ElasticSearchHandler($client, $handlerOpts); $handler = new ElasticsearchHandler($client, $handlerOpts);
if ($expectedError) { if ($expectedError) {
$this->expectException($expectedError[0]); $this->expectException($expectedError[0]);
@@ -163,18 +163,18 @@ class ElasticSearchHandlerTest extends TestCase
public function providerTestConnectionErrors() public function providerTestConnectionErrors()
{ {
return [ return [
[false, ['RuntimeException', 'Error sending messages to ElasticSearch']], [false, ['RuntimeException', 'Error sending messages to Elasticsearch']],
[true, false], [true, false],
]; ];
} }
/** /**
* Integration test using localhost Elastic Search server * Integration test using localhost Elasticsearch server
* *
* @covers Monolog\Handler\ElasticSearchHandler::__construct * @covers Monolog\Handler\ElasticsearchHandler::__construct
* @covers Monolog\Handler\ElasticSearchHandler::handleBatch * @covers Monolog\Handler\ElasticsearchHandler::handleBatch
* @covers Monolog\Handler\ElasticSearchHandler::bulkSend * @covers Monolog\Handler\ElasticsearchHandler::bulkSend
* @covers Monolog\Handler\ElasticSearchHandler::getDefaultFormatter * @covers Monolog\Handler\ElasticsearchHandler::getDefaultFormatter
*/ */
public function testHandleIntegration() public function testHandleIntegration()
{ {
@@ -200,12 +200,12 @@ class ElasticSearchHandlerTest extends TestCase
$client = ClientBuilder::create() $client = ClientBuilder::create()
->setHosts($hosts) ->setHosts($hosts)
->build(); ->build();
$handler = new ElasticSearchHandler($client, $this->options); $handler = new ElasticsearchHandler($client, $this->options);
try { try {
$handler->handleBatch([$msg]); $handler->handleBatch([$msg]);
} catch (\RuntimeException $e) { } catch (\RuntimeException $e) {
$this->markTestSkipped('Cannot connect to Elastic Search server on localhost'); $this->markTestSkipped('Cannot connect to Elasticsearch server on localhost');
} }
// check document id from ES server response // check document id from ES server response
@@ -229,7 +229,7 @@ class ElasticSearchHandlerTest extends TestCase
/** /**
* Return last created document id from ES response * Return last created document id from ES response
* *
* @param array $info ElasticSearch last request info * @param array $info Elasticsearch last request info
* @return string|null * @return string|null
*/ */
protected function getCreatedDocId(array $info) protected function getCreatedDocId(array $info)
@@ -244,7 +244,7 @@ class ElasticSearchHandlerTest extends TestCase
/** /**
* Retrieve document by id from Elasticsearch * Retrieve document by id from Elasticsearch
* *
* @param Client $client ElasticSearch client * @param Client $client Elasticsearch client
* @param string $index * @param string $index
* @param string $type * @param string $type
* @param string $documentId * @param string $documentId