Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 593b469

Browse files
authored
Merge pull request #49 from grayloon/api-logging
Add API Debug Logging
2 parents d80f290 + 9251f5c commit 593b469

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

config/magento.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,15 @@
6161
|
6262
*/
6363
'version' => env('MAGENTO_API_VERSION', 'V1'),
64+
65+
/*
66+
|--------------------------------------------------------------------------
67+
| Magento API Log Failed Requests
68+
|--------------------------------------------------------------------------
69+
|
70+
| Logs all non-successful Useful for debugging request parameters,
71+
| endpoints, or responses from the Magento API. Logs to the
72+
| Laravel Log file.
73+
*/
74+
'log_failed_requests' => env('MAGENTO_API_LOG_FAILED_REQUESTS', false),
6475
];

src/Api/AbstractApi.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Exception;
66
use Grayloon\Magento\Magento;
77
use Illuminate\Support\Facades\Http;
8+
use Illuminate\Support\Facades\Log;
89

910
abstract class AbstractApi
1011
{
@@ -57,7 +58,7 @@ protected function constructRequest()
5758
protected function get($path, $parameters = [])
5859
{
5960
return $this->checkExceptions(Http::withToken($this->magento->token)
60-
->get($this->apiRequest.$path, $parameters));
61+
->get($this->apiRequest.$path, $parameters), $this->apiRequest.$path, $parameters);
6162
}
6263

6364
/**
@@ -71,7 +72,7 @@ protected function get($path, $parameters = [])
7172
protected function post($path, $parameters = [])
7273
{
7374
return $this->checkExceptions(Http::withToken($this->magento->token)
74-
->post($this->apiRequest.$path, $parameters));
75+
->post($this->apiRequest.$path, $parameters), $this->apiRequest.$path, $parameters);
7576
}
7677

7778
/**
@@ -85,7 +86,7 @@ protected function post($path, $parameters = [])
8586
protected function put($path, $parameters = [])
8687
{
8788
return $this->checkExceptions(Http::withToken($this->magento->token)
88-
->put($this->apiRequest.$path, $parameters));
89+
->put($this->apiRequest.$path, $parameters), $this->apiRequest.$path, $parameters);
8990
}
9091

9192
/**
@@ -99,7 +100,7 @@ protected function put($path, $parameters = [])
99100
protected function delete($path, $parameters = [])
100101
{
101102
return $this->checkExceptions(Http::withToken($this->magento->token)
102-
->delete($this->apiRequest.$path, $parameters));
103+
->delete($this->apiRequest.$path, $parameters), $this->apiRequest.$path, $parameters);
103104
}
104105

105106
/**
@@ -109,12 +110,18 @@ protected function delete($path, $parameters = [])
109110
* @return void
110111
* @throws \Exception
111112
*/
112-
protected function checkExceptions($response)
113+
protected function checkExceptions($response, $endpoint, $parameters)
113114
{
114115
if ($response->serverError()) {
115116
throw new Exception($response['message'] ?? $response);
116117
}
117118

119+
if (! $response->successful()) {
120+
if (config('magento.log_failed_requests')) {
121+
Log::info('[MAGENTO API][STATUS] '.$response->status().' [ENDPOINT] '.$endpoint.' [PARAMETERS] '.json_encode($parameters).' [RESPONSE] '.json_encode($response->json()));
122+
}
123+
}
124+
118125
return $response;
119126
}
120127

0 commit comments

Comments
 (0)