Skip to content

Commit b924ec4

Browse files
committed
Added new logging of response and request headers
1 parent b5af4ac commit b924ec4

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

.github/workflows/php-cs-fixer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88

99
steps:
1010
- name: Checkout code
11-
uses: actions/checkout@v2
11+
uses: actions/checkout@v4
1212
with:
1313
ref: ${{ github.head_ref }}
1414

.github/workflows/phpstan.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414

1515
steps:
1616
- name: "Checkout"
17-
uses: actions/checkout@v2
17+
uses: actions/checkout@v4
1818

1919
- name: PHPStan
2020
uses: docker://oskarstark/phpstan-ga

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ You can install the package via composer:
1818
composer require always-open/laravel-request-logger
1919
```
2020

21+
### Breaking change
22+
If you are upgrading to 3.x or newer, the following steps must be taken as new fields have been added to the logging tables.
23+
- Create a migration for each logging tables with the following
24+
```php
25+
// The headers that were part of the request
26+
$table->json('request_headers')
27+
->nullable();
28+
// The headers that were part of the response
29+
$table->json('response_headers')
30+
->nullable();
31+
```
32+
Run this migration prior to upgrading the package.
33+
2134
## Configuration
2235

2336
``` php
@@ -74,6 +87,35 @@ function makeFacebookApiCall(array $body, Client $facebook_client)
7487
$request_log->save();
7588
}
7689
```
90+
91+
Instead of manually setting the response data you can instead leverage the `updateFromRequest` method:
92+
```php
93+
function makeFacebookApiCall(array $body, Client $facebook_client)
94+
{
95+
$request_headers = [
96+
'api-key' => $config->apiKey,
97+
'Content-Type' => 'application/json',
98+
];
99+
100+
$versioned_path = self::buildVersionedUrlPath($path);
101+
102+
$encoded_body = json_encode($body, JSON_UNESCAPED_SLASHES);
103+
104+
$request = new Request(
105+
'GET',
106+
'/v1/users',
107+
$request_headers,
108+
$encoded_body,
109+
);
110+
111+
$request_log = FacebookRequestLog::makeFromGuzzle($request);
112+
113+
$response = $client->send($request);
114+
$request_log->updateFromResponse($response);
115+
}
116+
```
117+
118+
77119
You can also manually set each property and then save the log instance.
78120

79121
### Testing

src/Models/RequestLogBaseModel.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use AlwaysOpen\RequestLogger\Observers\RequestLogObserver;
66
use GuzzleHttp\Psr7\Request;
77
use Illuminate\Database\Eloquent\Model;
8+
use Psr\Http\Message\ResponseInterface;
89

910
/**
1011
* AlwaysOpen\RequestLogger\Models\RequestLogBaseModel
@@ -14,7 +15,9 @@
1415
* @property string $http_method
1516
* @property int|null $response_code
1617
* @property array|string|null $body
18+
* @property array|string|null $request_headers
1719
* @property array|string|null $response
20+
* @property array|string|null $response_headers
1821
* @property string|null $exception
1922
* @property \Carbon\Carbon|null $occurred_at
2023
*/
@@ -25,7 +28,9 @@ class RequestLogBaseModel extends Model
2528
'created_at' => 'datetime',
2629
'updated_at' => 'datetime',
2730
'body' => 'json',
31+
'request_headers' => 'json',
2832
'response' => 'json',
33+
'response_headers' => 'json',
2934
];
3035

3136
protected $guarded = [
@@ -56,7 +61,18 @@ public static function makeFromGuzzle(Request $request) : static
5661
$instance->path = $request->getUri()->getPath();
5762
$instance->http_method = $request->getMethod();
5863
$instance->body = $request->getBody()->getContents();
64+
$instance->request_headers = $request->getHeaders();
5965

6066
return $instance;
6167
}
68+
69+
public function updateFromResponse(ResponseInterface $response): self {
70+
$this->response = json_decode($response->getBody()->getContents(), true);
71+
$this->response_code = $response->getStatusCode();
72+
$this->response_headers = $response->getHeaders();
73+
74+
$this->save();
75+
76+
return $this;
77+
}
6278
}

stubs/migration.stub

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class {CLASS_NAME} extends Migration
1919
$table->string('path', 191)
2020
->index();
2121
// What parameters were passed in (e.g. ?status=new)
22-
$table->string('params')
22+
$table->string('params', 512)
2323
->nullable()
2424
->fulltext();
2525
// HTTP method (e.g. POST/PUT/DELETE)
@@ -32,9 +32,15 @@ class {CLASS_NAME} extends Migration
3232
// The entire JSON encoded payload of the request
3333
$table->json('body')
3434
->nullable();
35+
// The headers that were part of the request
36+
$table->json('request_headers')
37+
->nullable();
3538
// The entire JSON encoded responses
3639
$table->json('response')
3740
->nullable();
41+
// The headers that were part of the response
42+
$table->json('response_headers')
43+
->nullable();
3844
// Internal exceptions that occurred during the request
3945
$table->string('exception')
4046
->nullable();

0 commit comments

Comments
 (0)