Skip to content

Commit a3a28b1

Browse files
authored
Merge pull request #950 from ezimuel/add-port-in-logger
Added the HTTP port in the log messages
2 parents 3334f67 + f22755b commit a3a28b1

File tree

4 files changed

+118
-15
lines changed

4 files changed

+118
-15
lines changed

src/Elasticsearch/Connections/Connection.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ public function logRequestSuccess(array $request, array $response): void
374374
array(
375375
'method' => $request['http_method'],
376376
'uri' => $response['effective_url'],
377+
'port' => $response['transfer_stats']['primary_port'],
377378
'headers' => $request['headers'],
378379
'HTTP code' => $response['status'],
379380
'duration' => $response['transfer_stats']['total_time'],
@@ -408,11 +409,13 @@ public function logRequestSuccess(array $request, array $response): void
408409
public function logRequestFail(array $request, array $response, \Exception $exception): void
409410
{
410411
$this->log->debug('Request Body', array($request['body']));
412+
411413
$this->log->warning(
412414
'Request Failure:',
413415
array(
414416
'method' => $request['http_method'],
415417
'uri' => $response['effective_url'],
418+
'port' => $response['transfer_stats']['primary_port'],
416419
'headers' => $request['headers'],
417420
'HTTP code' => $response['status'],
418421
'duration' => $response['transfer_stats']['total_time'],
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Elasticsearch\Tests\ClientBuilder;
5+
6+
use Psr\Log\LoggerInterface;
7+
use Psr\Log\LogLevel;
8+
9+
class ArrayLogger implements LoggerInterface
10+
{
11+
public $output = [];
12+
13+
public function emergency($message, array $context = array())
14+
{
15+
$this->log(LogLevel::EMERGENCY, $message, $context);
16+
}
17+
18+
public function alert($message, array $context = array())
19+
{
20+
$this->log(LogLevel::ALERT, $message, $context);
21+
}
22+
23+
public function critical($message, array $context = array())
24+
{
25+
$this->log(LogLevel::CRITICAL, $message, $context);
26+
}
27+
28+
public function error($message, array $context = array())
29+
{
30+
$this->log(LogLevel::ERROR, $message, $context);
31+
}
32+
33+
public function warning($message, array $context = array())
34+
{
35+
$this->log(LogLevel::WARNING, $message, $context);
36+
}
37+
38+
public function notice($message, array $context = array())
39+
{
40+
$this->log(LogLevel::NOTICE, $message, $context);
41+
}
42+
43+
public function info($message, array $context = array())
44+
{
45+
$this->log(LogLevel::INFO, $message, $context);
46+
}
47+
48+
public function debug($message, array $context = array())
49+
{
50+
$this->log(LogLevel::DEBUG, $message, $context);
51+
}
52+
53+
public function log($level, $message, array $context = array())
54+
{
55+
$this->output[] = sprintf("%s: %s %s", $level, $message, json_encode($context));
56+
}
57+
}

tests/Elasticsearch/Tests/ClientBuilder/DummyLogger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
class DummyLogger
77
{
8-
8+
99
}

tests/Elasticsearch/Tests/ClientIntegrationTests.php

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
namespace Elasticsearch\Tests;
66

7-
use Elasticsearch;
7+
use Elasticsearch\ClientBuilder;
8+
use Elasticsearch\Common\Exceptions\Missing404Exception;
9+
use Elasticsearch\Tests\ClientBuilder\ArrayLogger;
10+
use Psr\Log\LogLevel;
811

912
/**
1013
* Class ClientTest
@@ -18,22 +21,62 @@
1821
*/
1922
class ClientIntegrationTests extends \PHPUnit\Framework\TestCase
2023
{
21-
public function testCustomQueryParams()
24+
public function setUp()
2225
{
23-
$client = Elasticsearch\ClientBuilder::create()
26+
if (empty(getenv('ES_TEST_HOST'))) {
27+
$this->markTestSkipped('I cannot execute integration test without ES_TEST_HOST env');
28+
}
29+
$this->logger = new ArrayLogger();
30+
}
31+
32+
public function testLogRequestSuccessHasInfoNotEmpty()
33+
{
34+
$client = ClientBuilder::create()
35+
->setHosts([getenv('ES_TEST_HOST')])
36+
->setLogger($this->logger)
37+
->build();
38+
39+
$result = $client->info();
40+
41+
$this->assertNotEmpty($this->getLevelOutput(LogLevel::INFO, $this->logger->output));
42+
}
43+
44+
public function testLogRequestSuccessHasPortInInfo()
45+
{
46+
$client = ClientBuilder::create()
2447
->setHosts([getenv('ES_TEST_HOST')])
48+
->setLogger($this->logger)
2549
->build();
2650

27-
$getParams = [
28-
'index' => 'test',
29-
'type' => 'test',
30-
'id' => 1,
31-
'parent' => 'abc',
32-
'custom' => ['customToken' => 'abc', 'otherToken' => 123],
33-
'client' => ['ignore' => 400]
34-
];
35-
$exists = $client->exists($getParams);
36-
37-
$this->assertFalse((bool) $exists);
51+
$result = $client->info();
52+
53+
$this->assertContains('"port"', $this->getLevelOutput(LogLevel::INFO, $this->logger->output));
54+
}
55+
56+
public function testLogRequestFailHasWarning()
57+
{
58+
$client = ClientBuilder::create()
59+
->setHosts([getenv('ES_TEST_HOST')])
60+
->setLogger($this->logger)
61+
->build();
62+
63+
try {
64+
$result = $client->get([
65+
'index' => 'foo',
66+
'id' => 'bar'
67+
]);
68+
} catch (Missing404Exception $e) {
69+
$this->assertNotEmpty($this->getLevelOutput(LogLevel::WARNING, $this->logger->output));
70+
}
71+
}
72+
73+
private function getLevelOutput(string $level, array $output): string
74+
{
75+
foreach ($output as $out) {
76+
if (false !== strpos($out, $level)) {
77+
return $out;
78+
}
79+
}
80+
return '';
3881
}
3982
}

0 commit comments

Comments
 (0)