Skip to content

Commit

Permalink
Simplify the logRequest methods
Browse files Browse the repository at this point in the history
  • Loading branch information
emilgodsk committed Apr 22, 2019
1 parent 4b78667 commit e71868c
Showing 1 changed file with 36 additions and 91 deletions.
127 changes: 36 additions & 91 deletions src/Elasticsearch/Connections/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,7 @@ private function wrapHandler(callable $handler)

$exception = $this->getCurlRetryException($request, $response);

$this->logRequestFail(
$request['http_method'],
$response['effective_url'],
$request['body'],
$request['headers'],
$response['status'],
$response['body'],
$response['transfer_stats']['total_time'],
$exception
);
$this->logRequestFail($request, $response, $exception);

$node = $connection->getHost();
$this->log->warning("Marking node $node dead.");
Expand Down Expand Up @@ -254,16 +245,7 @@ private function wrapHandler(callable $handler)
} else {
// Something went seriously wrong, bail
$exception = new TransportException($response['error']->getMessage());
$this->logRequestFail(
$request['http_method'],
$response['effective_url'],
$request['body'],
$request['headers'],
$response['status'],
$response['body'],
$response['transfer_stats']['total_time'],
$exception
);
$this->logRequestFail($request, $response, $exception);
throw $exception;
}
} else {
Expand All @@ -285,15 +267,7 @@ private function wrapHandler(callable $handler)
// No error, deserialize
$response['body'] = $this->serializer->deserialize($response['body'], $response['transfer_stats']);
}
$this->logRequestSuccess(
$request['http_method'],
$response['effective_url'],
$request['body'],
$request['headers'],
$response['status'],
$response['body'],
$response['transfer_stats']['total_time']
);
$this->logRequestSuccess($request, $response);

return isset($request['client']['verbose']) && $request['client']['verbose'] === true ? $response : $response['body'];
});
Expand Down Expand Up @@ -332,87 +306,76 @@ private function getURI($uri, $params)
/**
* Log a successful request
*
* @param string $method
* @param string $fullURI
* @param string $body
* @param array $headers
* @param string $statusCode
* @param string $response
* @param string $duration
*
* @param array $request
* @param array $response
* @return void
*/
public function logRequestSuccess($method, $fullURI, $body, $headers, $statusCode, $response, $duration)
public function logRequestSuccess($request, $response)
{
$this->log->debug('Request Body', array($body));
$this->log->debug('Request Body', array($request['body']));
$this->log->info(
'Request Success:',
array(
'method' => $method,
'uri' => $fullURI,
'headers' => $headers,
'HTTP code' => $statusCode,
'duration' => $duration,
'method' => $request['http_method'],
'uri' => $response['effective_url'],
'headers' => $request['headers'],
'HTTP code' => $response['status'],
'duration' => $response['transfer_stats']['total_time'],
)
);
$this->log->debug('Response', array($response));
$this->log->debug('Response', array($response['body']));

// Build the curl command for Trace.
$curlCommand = $this->buildCurlCommand($method, $fullURI, $body);
$curlCommand = $this->buildCurlCommand($request['http_method'], $response['effective_url'], $request['body']);
$this->trace->info($curlCommand);
$this->trace->debug(
'Response:',
array(
'response' => $response,
'method' => $method,
'uri' => $fullURI,
'HTTP code' => $statusCode,
'duration' => $duration,
'response' => $response['body'],
'method' => $request['http_method'],
'uri' => $response['effective_url'],
'HTTP code' => $response['status'],
'duration' => $response['transfer_stats']['total_time'],
)
);
}

/**
* Log a failed request
*
* @param string $method
* @param string $fullURI
* @param string $body
* @param array $headers
* @param null|string $statusCode
* @param null|string $response
* @param string $duration
* @param array $request
* @param array $response
* @param \Exception $exception
*
* @return void
*/
public function logRequestFail($method, $fullURI, $body, $headers, $statusCode, $response, $duration, \Exception $exception)
public function logRequestFail($request, $response, \Exception $exception)
{
$this->log->debug('Request Body', array($body));
$this->log->debug('Request Body', array($request['body']));
$this->log->warning(
'Request Failure:',
array(
'method' => $method,
'uri' => $fullURI,
'headers' => $headers,
'HTTP code' => $statusCode,
'duration' => $duration,
'method' => $request['http_method'],
'uri' => $response['effective_url'],
'headers' => $request['headers'],
'HTTP code' => $response['status'],
'duration' => $response['transfer_stats']['total_time'],
'error' => $exception->getMessage(),
)
);
$this->log->warning('Response', array($response));
$this->log->warning('Response', array($response['body']));

// Build the curl command for Trace.
$curlCommand = $this->buildCurlCommand($method, $fullURI, $body);
$curlCommand = $this->buildCurlCommand($request['http_method'], $response['effective_url'], $request['body']);
$this->trace->info($curlCommand);
$this->trace->debug(
'Response:',
array(
'response' => $response,
'method' => $method,
'uri' => $fullURI,
'HTTP code' => $statusCode,
'duration' => $duration,
'method' => $request['http_method'],
'uri' => $response['effective_url'],
'HTTP code' => $response['status'],
'duration' => $response['transfer_stats']['total_time'],
)
);
}
Expand Down Expand Up @@ -620,16 +583,7 @@ private function process4xxError($request, $response, $ignore)
$exception = new BadRequest400Exception($responseBody, $statusCode);
}

$this->logRequestFail(
$request['http_method'],
$response['effective_url'],
$request['body'],
$request['headers'],
$response['status'],
$response['body'],
$response['transfer_stats']['total_time'],
$exception
);
$this->logRequestFail($request, $response, $exception);

throw $exception;
}
Expand Down Expand Up @@ -666,16 +620,7 @@ private function process5xxError($request, $response, $ignore)
$exception = new ServerErrorResponseException($responseBody, $statusCode);
}

$this->logRequestFail(
$request['http_method'],
$response['effective_url'],
$request['body'],
$request['headers'],
$response['status'],
$response['body'],
$response['transfer_stats']['total_time'],
$exception
);
$this->logRequestFail($request, $response, $exception);

throw $exception;
}
Expand Down

0 comments on commit e71868c

Please sign in to comment.