Skip to content

chore: use logger context #657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion MangoPay/Libraries/HttpCurl.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ private function RunRequest()
{
$result = curl_exec($this->_curlHandle);
if ($result === false && curl_errno($this->_curlHandle) != 0) {
$this->logger->error("cURL error: " . curl_error($this->_curlHandle));
$this->logger->error('cURL error', [
'errno' => curl_errno($this->_curlHandle),
'error' => curl_error($this->_curlHandle)
]);
throw new Exception('cURL error: ' . curl_error($this->_curlHandle));
}

Expand Down
99 changes: 54 additions & 45 deletions MangoPay/Libraries/RestTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ public function GetRequestHeaders()
}

/**
* Request type for current request
* @var RequestType
* Request type for current request (HTTP method).
* One of \MangoPay\Libraries\RequestType constants
* @var string
*/
private $_requestType;

/**
* Return HTTP request method
* @return RequestType
* @return string
*/
public function GetRequestType()
{
Expand Down Expand Up @@ -124,7 +125,7 @@ public function AddRequestHttpHeader($httpHeader)
/**
* Call request to MangoPay API
* @param string $urlMethod Type of method in REST API
* @param \MangoPay\Libraries\RequestType $requestType Type of request
* @param string $requestType Type of request (constant of \MangoPay\Libraries\RequestType)
* @param array $requestData Data to send in request
* @param string $idempotencyKey
* @param \MangoPay\Pagination $pagination Pagination object
Expand All @@ -139,25 +140,23 @@ public function Request($urlMethod, $requestType, $requestData = null, $idempote
&& (strpos($urlMethod, 'KYC/documents') !== false || strpos($urlMethod, 'dispute-documents') !== false)) {
$this->_requestData = "";
}
$logClass = $this->_root->Config->LogClass;
$this->logger->debug("New request");
if ($this->_root->Config->DebugMode) {
$logClass::Debug('++++++++++++++++++++++ New request ++++++++++++++++++++++', '');
}

$this->debug('++++++++++++++++++++++ New request ++++++++++++++++++++++', '');

$this->BuildRequest($urlMethod, $pagination, $additionalUrlParams, $idempotencyKey);
$responseResult = $this->_root->getHttpClient()->Request($this);
$logClass = $this->_root->Config->LogClass;
$this->logger->debug('Response JSON : ' . print_r($responseResult->Body, true));
if ($this->_root->Config->DebugMode) {
$logClass::Debug('Response JSON', $responseResult->Body);
}
$this->debug('Response JSON', $responseResult->Body);

// FIXME This can fail hard.
$response = json_decode($responseResult->Body);
$this->debug('Response object', $response);

$this->logger->info('Response received', [
'responseCode' => $responseResult->ResponseCode,
'headers' => $responseResult->Headers,
'body' => $response ?: $responseResult->Body,
]);

$this->logger->debug('Decoded object : ' . print_r($response, true));
if ($this->_root->Config->DebugMode) {
$logClass::Debug('Response object', $response);
}
$this->CheckResponseCode($responseResult->ResponseCode, $response);
$this->ReadResponseHeader($responseResult->Headers);
if (!is_null($pagination)) {
Expand All @@ -179,36 +178,34 @@ private function BuildRequest($urlMethod, $pagination, $additionalUrlParams = nu
$urlTool = new UrlTool($this->_root);
$restUrl = $urlTool->GetRestUrl($urlMethod, $this->_clientIdRequired, $pagination, $additionalUrlParams);
$this->_requestUrl = $urlTool->GetFullUrl($restUrl);
$logClass = $this->_root->Config->LogClass;
$this->logger->debug('FullUrl : ' . $this->_requestUrl);
if ($this->_root->Config->DebugMode) {
$logClass::Debug('FullUrl', $this->_requestUrl);
}

$this->debug('FullUrl', $this->_requestUrl);
$this->debug('RequestType', $this->_requestType);

if (!is_null($pagination)) {
$this->_pagination = $pagination;
}
$this->logger->debug('RequestType : ' . $this->_requestType);
if ($this->_root->Config->DebugMode) {
$logClass::Debug('RequestType', $this->_requestType);
}

$httpHeaders = $this->GetHttpHeaders($idempotencyKey);
$this->logger->debug('HTTP Headers : ' . print_r($httpHeaders, true));
if ($this->_root->Config->DebugMode) {
$logClass::Debug('HTTP Headers', $httpHeaders);
}
$this->debug('HTTP Headers', $httpHeaders);

$this->logger->info('Building request', [
'urlMethod' => $urlMethod,
'fullUrl' => $this->_requestUrl,
'requestType' => $this->_requestType,
'headers' => $httpHeaders,
'requestData' => $this->_requestData,
'json' => in_array(self::$_JSON_HEADER, $httpHeaders, true),
]);

if (!is_null($this->_requestData)) {
$this->logger->debug('RequestData object :' . print_r($this->_requestData, true));
if ($this->_root->Config->DebugMode) {
$logClass::Debug('RequestData object', $this->_requestData);
}
$this->debug('RequestData object', $this->_requestData);

// encode to json if needed
if (in_array(self::$_JSON_HEADER, $httpHeaders)) {
if (!is_null($this->_requestData)) {
$this->_requestData = json_encode($this->_requestData);
$this->logger->debug('RequestData JSON :' . print_r($this->_requestData, true));
if ($this->_root->Config->DebugMode) {
$logClass::Debug('RequestData JSON', $this->_requestData);
}
$this->debug('RequestData JSON', $this->_requestData);
}
}
}
Expand All @@ -220,12 +217,7 @@ private function BuildRequest($urlMethod, $pagination, $additionalUrlParams = nu
*/
private function ReadResponseHeader($headers)
{
$logClass = $this->_root->Config->LogClass;

$this->logger->debug('Response headers :' . print_r($headers, true));
if ($this->_root->Config->DebugMode) {
$logClass::Debug('Response headers', print_r($headers, true));
}
$this->debug('Response headers', print_r($headers, true));

$updatedRateLimits = null;

Expand Down Expand Up @@ -397,4 +389,21 @@ private function CheckResponseCode($responseCode, $response)

throw new ResponseException($this->_requestUrl, $responseCode, $error);
}

/**
* Prints debug message & context if debug mode is enabled.
*
* @param string $message
* @param mixed $data
* @return void
*/
private function debug($message, $data = null)
{
if (!$this->_root->Config->DebugMode) {
return;
}

$logClass = $this->_root->Config->LogClass;
$logClass::Debug($message, $data);
}
}