Skip to content

Commit

Permalink
Merge pull request #950 from ezimuel/add-port-in-logger
Browse files Browse the repository at this point in the history
Added the HTTP port in the log messages
  • Loading branch information
philkra authored Sep 23, 2019
2 parents 3334f67 + f22755b commit a3a28b1
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/Elasticsearch/Connections/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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'],
Expand Down
57 changes: 57 additions & 0 deletions tests/Elasticsearch/Tests/ClientBuilder/ArrayLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
declare(strict_types = 1);

namespace Elasticsearch\Tests\ClientBuilder;

use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

class ArrayLogger implements LoggerInterface
{
public $output = [];

public function emergency($message, array $context = array())
{
$this->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));
}
}
2 changes: 1 addition & 1 deletion tests/Elasticsearch/Tests/ClientBuilder/DummyLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

class DummyLogger
{

}
71 changes: 57 additions & 14 deletions tests/Elasticsearch/Tests/ClientIntegrationTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 '';
}
}

0 comments on commit a3a28b1

Please sign in to comment.