Skip to content

Commit 16c8bf4

Browse files
committed
Update Client to mitigate _log being overwritten
1 parent 03bddff commit 16c8bf4

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file based on the
44
## [Unreleased](https://github.com/ruflin/Elastica/compare/3.1.1...HEAD)
55

66
### Backward Compatibility Breaks
7+
- \Elastica\Client::_log now has a second parameter for passing a Response for logging successful Requests. Note that overriding _log is now deprecated and customisation to logging should be handled with a custom LoggerInterface.
78

89
### Bugfixes
910
- Fix php notice on `\Elastica\Index::getAliases()` if index has no aliases #1078
@@ -16,7 +17,7 @@ All notable changes to this project will be documented in this file based on the
1617
- Elastica\Client constructor now accepts a LoggerInterface and will log both successful and failed requests. #1069
1718

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

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

lib/Elastica/Client.php

+37-10
Original file line numberDiff line numberDiff line change
@@ -642,11 +642,7 @@ public function request($path, $method = Request::GET, $data = array(), array $q
642642
} catch (ConnectionException $e) {
643643
$this->_connectionPool->onFail($connection, $e, $this);
644644

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

651647
// In case there is no valid connection left, throw exception which caused the disabling of the connection.
652648
if (!$this->hasConnection()) {
@@ -656,15 +652,46 @@ public function request($path, $method = Request::GET, $data = array(), array $q
656652
return $this->request($path, $method, $data, $query);
657653
}
658654

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

665657
return $response;
666658
}
667659

660+
/**
661+
* logging.
662+
*
663+
* @deprecated Overwriting Client->_log is deprecated. Handle logging functionality by using a custom LoggerInterface.
664+
*
665+
* @param mixed $context
666+
* @param Response $response
667+
*/
668+
protected function _log($context, Response $response = null)
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' => $response ? $response->getData() : null,
684+
'responseStatus' => $response ? $response->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)