Skip to content

Commit

Permalink
Update Client to mitigate _log being overwritten
Browse files Browse the repository at this point in the history
  • Loading branch information
merk committed Apr 26, 2016
1 parent 03bddff commit 16c8bf4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file based on the
## [Unreleased](https://github.com/ruflin/Elastica/compare/3.1.1...HEAD)

### Backward Compatibility Breaks
- \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.

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

### Deprecated

- Configuring the logger in \Elastica\Client $config constructor is deprecated and will be removed. Use the $logger argument instead.

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

Expand Down
47 changes: 37 additions & 10 deletions lib/Elastica/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -642,11 +642,7 @@ public function request($path, $method = Request::GET, $data = array(), array $q
} catch (ConnectionException $e) {
$this->_connectionPool->onFail($connection, $e, $this);

$this->_logger->error('Elastica Request Failure', [
'exception' => $e,
'request' => $request->toArray(),
'retry' => $this->hasConnection()
]);
$this->_log($e, $request);

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

$this->_logger->debug('Elastica Request', [
'request' => $request->toArray(),
'response' => $response->getData(),
'responseStatus' => $response->getStatus()
]);
$this->_log($request, $response);

return $response;
}

/**
* logging.
*
* @deprecated Overwriting Client->_log is deprecated. Handle logging functionality by using a custom LoggerInterface.
*
* @param mixed $context
* @param Response $response
*/
protected function _log($context, Response $response = null)
{
if ($context instanceof ConnectionException) {
$this->_logger->error('Elastica Request Failure', [
'exception' => $context,
'request' => $context->getRequest()->toArray(),
'retry' => $this->hasConnection()
]);

return;
}

if ($context instanceof Request) {
$this->_logger->debug('Elastica Request', [
'request' => $context->toArray(),
'response' => $response ? $response->getData() : null,
'responseStatus' => $response ? $response->getStatus() : null
]);

return;
}

$this->_logger->debug('Elastica Request', [
'message' => $context
]);
}

/**
* Optimizes all search indices.
*
Expand Down

0 comments on commit 16c8bf4

Please sign in to comment.