diff --git a/src/Elasticsearch/Connections/Connection.php b/src/Elasticsearch/Connections/Connection.php index 72dfc345c..540bcfe15 100644 --- a/src/Elasticsearch/Connections/Connection.php +++ b/src/Elasticsearch/Connections/Connection.php @@ -374,6 +374,7 @@ public function logRequestSuccess(array $request, array $response): void array( 'method' => $request['http_method'], 'uri' => $response['effective_url'], + 'port' => $response['transfer_stats']['primary_port'], 'headers' => $request['headers'], 'HTTP code' => $response['status'], 'duration' => $response['transfer_stats']['total_time'], @@ -408,11 +409,13 @@ public function logRequestSuccess(array $request, array $response): void public function logRequestFail(array $request, array $response, \Exception $exception): void { $this->log->debug('Request Body', array($request['body'])); + $this->log->warning( 'Request Failure:', array( 'method' => $request['http_method'], 'uri' => $response['effective_url'], + 'port' => $response['transfer_stats']['primary_port'], 'headers' => $request['headers'], 'HTTP code' => $response['status'], 'duration' => $response['transfer_stats']['total_time'], diff --git a/tests/Elasticsearch/Tests/ClientBuilder/ArrayLogger.php b/tests/Elasticsearch/Tests/ClientBuilder/ArrayLogger.php new file mode 100644 index 000000000..3a9010794 --- /dev/null +++ b/tests/Elasticsearch/Tests/ClientBuilder/ArrayLogger.php @@ -0,0 +1,57 @@ +log(LogLevel::EMERGENCY, $message, $context); + } + + public function alert($message, array $context = array()) + { + $this->log(LogLevel::ALERT, $message, $context); + } + + public function critical($message, array $context = array()) + { + $this->log(LogLevel::CRITICAL, $message, $context); + } + + public function error($message, array $context = array()) + { + $this->log(LogLevel::ERROR, $message, $context); + } + + public function warning($message, array $context = array()) + { + $this->log(LogLevel::WARNING, $message, $context); + } + + public function notice($message, array $context = array()) + { + $this->log(LogLevel::NOTICE, $message, $context); + } + + public function info($message, array $context = array()) + { + $this->log(LogLevel::INFO, $message, $context); + } + + public function debug($message, array $context = array()) + { + $this->log(LogLevel::DEBUG, $message, $context); + } + + public function log($level, $message, array $context = array()) + { + $this->output[] = sprintf("%s: %s %s", $level, $message, json_encode($context)); + } +} diff --git a/tests/Elasticsearch/Tests/ClientBuilder/DummyLogger.php b/tests/Elasticsearch/Tests/ClientBuilder/DummyLogger.php index f5c536c77..6887853c2 100644 --- a/tests/Elasticsearch/Tests/ClientBuilder/DummyLogger.php +++ b/tests/Elasticsearch/Tests/ClientBuilder/DummyLogger.php @@ -5,5 +5,5 @@ class DummyLogger { - + } diff --git a/tests/Elasticsearch/Tests/ClientIntegrationTests.php b/tests/Elasticsearch/Tests/ClientIntegrationTests.php index 55bc3b456..cf36725fe 100644 --- a/tests/Elasticsearch/Tests/ClientIntegrationTests.php +++ b/tests/Elasticsearch/Tests/ClientIntegrationTests.php @@ -4,7 +4,10 @@ namespace Elasticsearch\Tests; -use Elasticsearch; +use Elasticsearch\ClientBuilder; +use Elasticsearch\Common\Exceptions\Missing404Exception; +use Elasticsearch\Tests\ClientBuilder\ArrayLogger; +use Psr\Log\LogLevel; /** * Class ClientTest @@ -18,22 +21,62 @@ */ class ClientIntegrationTests extends \PHPUnit\Framework\TestCase { - public function testCustomQueryParams() + public function setUp() { - $client = Elasticsearch\ClientBuilder::create() + if (empty(getenv('ES_TEST_HOST'))) { + $this->markTestSkipped('I cannot execute integration test without ES_TEST_HOST env'); + } + $this->logger = new ArrayLogger(); + } + + public function testLogRequestSuccessHasInfoNotEmpty() + { + $client = ClientBuilder::create() + ->setHosts([getenv('ES_TEST_HOST')]) + ->setLogger($this->logger) + ->build(); + + $result = $client->info(); + + $this->assertNotEmpty($this->getLevelOutput(LogLevel::INFO, $this->logger->output)); + } + + public function testLogRequestSuccessHasPortInInfo() + { + $client = ClientBuilder::create() ->setHosts([getenv('ES_TEST_HOST')]) + ->setLogger($this->logger) ->build(); - $getParams = [ - 'index' => 'test', - 'type' => 'test', - 'id' => 1, - 'parent' => 'abc', - 'custom' => ['customToken' => 'abc', 'otherToken' => 123], - 'client' => ['ignore' => 400] - ]; - $exists = $client->exists($getParams); - - $this->assertFalse((bool) $exists); + $result = $client->info(); + + $this->assertContains('"port"', $this->getLevelOutput(LogLevel::INFO, $this->logger->output)); + } + + public function testLogRequestFailHasWarning() + { + $client = ClientBuilder::create() + ->setHosts([getenv('ES_TEST_HOST')]) + ->setLogger($this->logger) + ->build(); + + try { + $result = $client->get([ + 'index' => 'foo', + 'id' => 'bar' + ]); + } catch (Missing404Exception $e) { + $this->assertNotEmpty($this->getLevelOutput(LogLevel::WARNING, $this->logger->output)); + } + } + + private function getLevelOutput(string $level, array $output): string + { + foreach ($output as $out) { + if (false !== strpos($out, $level)) { + return $out; + } + } + return ''; } }