diff --git a/MangoPay/Libraries/ApiBase.php b/MangoPay/Libraries/ApiBase.php index d5a5c48d..a92fc3ad 100644 --- a/MangoPay/Libraries/ApiBase.php +++ b/MangoPay/Libraries/ApiBase.php @@ -11,6 +11,14 @@ abstract class ApiBase { * @var \MangoPay\MangoPayApi */ protected $_root; + + /** + * @return mixed + */ + protected function getLogger() + { + return $this->_root->getLogger(); + } /** * Array with REST url and request type diff --git a/MangoPay/Libraries/RestTool.php b/MangoPay/Libraries/RestTool.php index dd5a9f78..7b003b16 100644 --- a/MangoPay/Libraries/RestTool.php +++ b/MangoPay/Libraries/RestTool.php @@ -1,5 +1,6 @@ _authRequired = $authRequired; $this->_root = $root; + $this->logger = $root->getLogger(); } public function AddRequestHttpHeader($httpHeader) { @@ -91,13 +98,15 @@ public function Request($urlMethod, $requestType, $requestData = null, $idempote $this->_requestData = $requestData; $logClass = $this->_root->Config->LogClass; - if ($this->_root->Config->DebugMode) + $this->logger->debug("New request"); + if ($this->_root->Config->DebugMode) { $logClass::Debug('++++++++++++++++++++++ New request ++++++++++++++++++++++', ''); + } $this->BuildRequest($urlMethod, $pagination, $additionalUrlParams, $idempotencyKey); $responseResult = $this->RunRequest(); - if(!is_null($pagination)){ + if(!is_null($pagination)) { $pagination = $this->_pagination; } @@ -112,22 +121,31 @@ public function Request($urlMethod, $requestType, $requestData = null, $idempote private function RunRequest() { $result = curl_exec($this->_curlHandle); - if ($result === false && curl_errno($this->_curlHandle) != 0) + if ($result === false && curl_errno($this->_curlHandle) != 0) { + $this->logger->error("cURL error: " . curl_error($this->_curlHandle)); + throw new Exception('cURL error: ' . curl_error($this->_curlHandle)); + } - $this->_responseCode = (int)curl_getinfo($this->_curlHandle, CURLINFO_HTTP_CODE); + $this->_responseCode = (int) curl_getinfo($this->_curlHandle, CURLINFO_HTTP_CODE); curl_close($this->_curlHandle); $logClass = $this->_root->Config->LogClass; - if ($this->_root->Config->DebugMode) + + $this->logger->debug('Response JSON : ' . print_r($result, true)); + if ($this->_root->Config->DebugMode) { $logClass::Debug('Response JSON', $result); - - $response = json_decode($result); - - if ($this->_root->Config->DebugMode) + } + + // FIXME This can fail hard. + $response = json_decode($result); + + $this->logger->debug('Decoded object : ' . print_r($response, true)); + if ($this->_root->Config->DebugMode) { $logClass::Debug('Response object', $response); - + } + $this->CheckResponseCode($response); return $response; @@ -145,12 +163,16 @@ private function BuildRequest($urlMethod, $pagination, $additionalUrlParams = nu $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->_curlHandle = curl_init($this->_requestUrl); if ($this->_curlHandle === false){ + $this->logger->error('Cannot initialize cURL session'); + throw new Exception('Cannot initialize cURL session'); } @@ -179,26 +201,38 @@ private function BuildRequest($urlMethod, $pagination, $additionalUrlParams = nu curl_setopt($this->_curlHandle, CURLOPT_CUSTOMREQUEST, "DELETE"); break; } - - if ($this->_root->Config->DebugMode) + + $this->logger->debug('RequestType : ' . $this->_requestType); + if ($this->_root->Config->DebugMode) { $logClass::Debug('RequestType', $this->_requestType); + } $httpHeaders = $this->GetHttpHeaders(); if ($idempotencyKey != null) array_push($httpHeaders, 'Idempotency-Key: ' . $idempotencyKey); curl_setopt($this->_curlHandle, CURLOPT_HTTPHEADER, $httpHeaders); - if ($this->_root->Config->DebugMode) + + $this->logger->debug('HTTP Headers : ' . print_r($httpHeaders, true)); + if ($this->_root->Config->DebugMode) { $logClass::Debug('HTTP Headers', $httpHeaders); + } if (!is_null($this->_requestData)) { - if ($this->_root->Config->DebugMode) + $this->logger->debug('RequestData object :' . print_r($this->_requestData, true)); + if ($this->_root->Config->DebugMode) { $logClass::Debug('RequestData object', $this->_requestData); + } // encode to json if needed if (in_array(self::$_JSON_HEADER, $httpHeaders)) { + + // FIXME This can also fail hard and is not checked. $this->_requestData = json_encode($this->_requestData); - if ($this->_root->Config->DebugMode) + + $this->logger->debug('RequestData JSON :' . print_r($this->_requestData, true)); + if ($this->_root->Config->DebugMode) { $logClass::Debug('RequestData JSON', $this->_requestData); + } } curl_setopt($this->_curlHandle, CURLOPT_POSTFIELDS, $this->_requestData); @@ -214,6 +248,8 @@ private function BuildRequest($urlMethod, $pagination, $additionalUrlParams = nu private function ReadResponseHeader($handle, $header) { $logClass = $this->_root->Config->LogClass; + + $this->logger->debug('Response headers :' . $header); if ($this->_root->Config->DebugMode) $logClass::Debug('Response headers', $header); diff --git a/MangoPay/MangoPayApi.php b/MangoPay/MangoPayApi.php index 47b2d203..c319e3c9 100644 --- a/MangoPay/MangoPayApi.php +++ b/MangoPay/MangoPayApi.php @@ -1,6 +1,9 @@ KycDocuments = new ApiKycDocuments($this); $this->Disputes = new ApiDisputes($this); $this->DisputeDocuments = new ApiDisputeDocuments($this); + + // Setting default NullLogger + $this->logger = new NullLogger(); + + } + + /** + * @param LoggerInterface $logger + */ + public function setLogger(LoggerInterface $logger) + { + $this->logger = $logger; + } + + /** + * @return LoggerInterface + */ + public function getLogger() + { + return $this->logger; } } \ No newline at end of file diff --git a/README.md b/README.md index 5540b0d5..9a229ce3 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ Step 2 - Update your dependencies with Composer The Library has been added into your dependencies and ready to be used. + License ------------------------------------------------- MangopaySDK is distributed under MIT license, see LICENSE file. @@ -175,3 +176,22 @@ class MangoPayService } } ``` + + +Logging +------- +MangoPay uses the PSR3 LoggerInterface. You can provide your own logger to the API. +Here is a sample showing Monolog integration : + +```php +use Monolog\Logger; +use Monolog\Handler\StreamHandler; + +... + +$logger = new Logger('sample-logger'); +$logger->pushHandler(new StreamHandler($logConfig['path'], Logger::DEBUG)); + +$this->mangoPayApi = new MangoPay\MangoPayApi(); +$this->mangoPayApi->setLogger($logger); +``` \ No newline at end of file diff --git a/composer.json b/composer.json index 24d766b5..e54a8e1a 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ "require": { "php": ">=5.5.0", "ext-curl": "*", - "ext-openssl": "*" + "ext-openssl": "*", + "psr/log": "^1.0" }, "autoload": { "psr-4": { "MangoPay\\": "" },