Skip to content

Commit 60e805a

Browse files
committed
Update Client to mitigate _log being overwritten
1 parent 03bddff commit 60e805a

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ All notable changes to this project will be documented in this file based on the
1616
- Elastica\Client constructor now accepts a LoggerInterface and will log both successful and failed requests. #1069
1717

1818
### Deprecated
19-
19+
- Configuring the logger in \Elastica\Client $config constructor is deprecated and will be removed. Use the $logger argument instead.
2020

2121
## [3.1.1](https://github.com/ruflin/Elastica/compare/3.1.0...3.1.1)
2222

lib/Elastica/Client.php

+37-10
Original file line numberDiff line numberDiff line change
@@ -636,17 +636,14 @@ public function request($path, $method = Request::GET, $data = array(), array $q
636636
{
637637
$connection = $this->getConnection();
638638
$request = $this->_lastRequest = new Request($path, $method, $data, $query, $connection);
639+
$this->_lastResponse = null;
639640

640641
try {
641642
$response = $this->_lastResponse = $request->send();
642643
} catch (ConnectionException $e) {
643644
$this->_connectionPool->onFail($connection, $e, $this);
644645

645-
$this->_logger->error('Elastica Request Failure', [
646-
'exception' => $e,
647-
'request' => $request->toArray(),
648-
'retry' => $this->hasConnection()
649-
]);
646+
$this->_log($e);
650647

651648
// In case there is no valid connection left, throw exception which caused the disabling of the connection.
652649
if (!$this->hasConnection()) {
@@ -656,15 +653,45 @@ public function request($path, $method = Request::GET, $data = array(), array $q
656653
return $this->request($path, $method, $data, $query);
657654
}
658655

659-
$this->_logger->debug('Elastica Request', [
660-
'request' => $request->toArray(),
661-
'response' => $response->getData(),
662-
'responseStatus' => $response->getStatus()
663-
]);
656+
$this->_log($request, $response);
664657

665658
return $response;
666659
}
667660

661+
/**
662+
* logging.
663+
*
664+
* @deprecated Overwriting Client->_log is deprecated. Handle logging functionality by using a custom LoggerInterface.
665+
*
666+
* @param mixed $context
667+
*/
668+
protected function _log($context)
669+
{
670+
if ($context instanceof ConnectionException) {
671+
$this->_logger->error('Elastica Request Failure', [
672+
'exception' => $context,
673+
'request' => $context->getRequest()->toArray(),
674+
'retry' => $this->hasConnection()
675+
]);
676+
677+
return;
678+
}
679+
680+
if ($context instanceof Request) {
681+
$this->_logger->debug('Elastica Request', [
682+
'request' => $context->toArray(),
683+
'response' => $this->_lastResponse ? $this->_lastResponse->getData() : null,
684+
'responseStatus' => $this->_lastResponse ? $this->_lastResponse->getStatus() : null
685+
]);
686+
687+
return;
688+
}
689+
690+
$this->_logger->debug('Elastica Request', [
691+
'message' => $context
692+
]);
693+
}
694+
668695
/**
669696
* Optimizes all search indices.
670697
*

0 commit comments

Comments
 (0)