Skip to content

Commit 1d5a54f

Browse files
committed
Pass through logger from client
1 parent a7ec564 commit 1d5a54f

4 files changed

Lines changed: 39 additions & 8 deletions

File tree

src/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ public function request(string $path, string $method = Request::GET, $data = [],
578578
}
579579

580580
try {
581-
$response = $this->_lastResponse = $request->send();
581+
$response = $this->_lastResponse = $request->send($this->_logger);
582582
} catch (ConnectionException $e) {
583583
$this->_connectionPool->onFail($connection, $e, $this);
584584
$this->_logger->error(sprintf('Elastica Request Failure %s', $requestName), [

src/Connection.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Elastica\Exception\InvalidException;
66
use Elastica\Transport\AbstractTransport;
7+
use Psr\Log\LoggerInterface;
8+
use Psr\Log\NullLogger;
79

810
/**
911
* Elastica connection instance to an elasticasearch node.
@@ -248,11 +250,11 @@ public function isEnabled()
248250
*
249251
* @return AbstractTransport Transport object
250252
*/
251-
public function getTransportObject()
253+
public function getTransportObject(LoggerInterface $logger)
252254
{
253255
$transport = $this->getTransport();
254256

255-
return AbstractTransport::create($transport, $this);
257+
return AbstractTransport::create($transport, $this, [], $logger = new NullLogger());
256258
}
257259

258260
/**

src/Request.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Elastica\Exception\ConnectionException;
66
use Elastica\Exception\InvalidException;
77
use Elastica\Exception\ResponseException;
8+
use Psr\Log\LoggerInterface;
89

910
/**
1011
* Elastica Request object.
@@ -175,9 +176,9 @@ public function getContentType(): string
175176
* @throws ResponseException
176177
* @throws ConnectionException
177178
*/
178-
public function send(): Response
179+
public function send(LoggerInterface $logger): Response
179180
{
180-
$transport = $this->getConnection()->getTransportObject();
181+
$transport = $this->getConnection()->getTransportObject($logger);
181182

182183
// Refactor: Not full toArray needed in exec?
183184
return $transport->exec($this, $this->getConnection()->toArray());

src/Transport/AbstractTransport.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Elastica\Param;
1010
use Elastica\Request;
1111
use Elastica\Response;
12+
use Psr\Log\LoggerInterface;
1213

1314
/**
1415
* Elastica Abstract Transport object.
@@ -22,14 +23,22 @@ abstract class AbstractTransport extends Param
2223
*/
2324
protected $_connection;
2425

26+
/**
27+
* @var LoggerInterface
28+
*/
29+
protected $_logger;
30+
2531
/**
2632
* Construct transport.
2733
*/
28-
public function __construct(?Connection $connection = null)
34+
public function __construct(LoggerInterface $logger, ?Connection $connection = null)
2935
{
3036
if ($connection) {
3137
$this->setConnection($connection);
3238
}
39+
if ($logger) {
40+
$this->setLogger($logger);
41+
}
3342
}
3443

3544
public function getConnection(): Connection
@@ -47,6 +56,21 @@ public function setConnection(Connection $connection): AbstractTransport
4756
return $this;
4857
}
4958

59+
public function getLogger(): LoggerInterface
60+
{
61+
return $this->_logger;
62+
}
63+
64+
/**
65+
* @return $this
66+
*/
67+
public function setLogger(LoggerInterface $logger): AbstractTransport
68+
{
69+
$this->_logger = $logger;
70+
71+
return $this;
72+
}
73+
5074
/**
5175
* Executes the transport request.
5276
*
@@ -93,7 +117,7 @@ public function sanityzeQueryStringBool(array $query)
93117
*
94118
* @throws InvalidException
95119
*/
96-
public static function create($transport, Connection $connection, array $params = []): AbstractTransport
120+
public static function create($transport, Connection $connection, array $params = [], LoggerInterface $logger = null): AbstractTransport
97121
{
98122
if (\is_array($transport) && isset($transport['type'])) {
99123
$transportParams = $transport;
@@ -113,7 +137,11 @@ public static function create($transport, Connection $connection, array $params
113137
$classNames = ["Elastica\\Transport\\{$transport}", $transport];
114138
foreach ($classNames as $className) {
115139
if (\class_exists($className)) {
116-
$transport = new $className();
140+
if ($transport === 'Http') {
141+
$transport = new $className($logger, $connection);
142+
} else {
143+
$transport = new $className();
144+
}
117145
break;
118146
}
119147
}

0 commit comments

Comments
 (0)