From 99a3234567e1e5784b18526081f9b3cd3784503d Mon Sep 17 00:00:00 2001 From: Quentin Schmick Date: Wed, 3 Sep 2025 08:51:24 -0400 Subject: [PATCH 1/6] Bumping php and laravel versions for GHA --- .github/workflows/run-tests.yml | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 846d52f..51ea256 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -13,19 +13,18 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest] - php: [8.0, 8.1] - laravel: [8.*, 9.*, 10.*] + php: [8.2, 8.3, 8.4] + laravel: [11.*, 12.*] stability: [prefer-stable] include: - - laravel: 8.* - - laravel: 9.* - - laravel: 10.* + - laravel: 11.* + - laravel: 12.* name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 From b5af4ac74eb879e9e7565235e65762088f22da46 Mon Sep 17 00:00:00 2001 From: Quentin Schmick Date: Wed, 3 Sep 2025 09:07:31 -0400 Subject: [PATCH 2/6] Bumping dep versions --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 899011c..c075586 100644 --- a/composer.json +++ b/composer.json @@ -23,8 +23,8 @@ } ], "require": { - "php": "^8.0.0|^8.1.0", - "always-open/laravel-process-stamps": "^5.0|^6.0|^7.0", + "php": "^8.2.0|^8.3.0|^8.4.0", + "always-open/laravel-process-stamps": "^5.0|^6.0|^7.0|^8.0", "guzzlehttp/guzzle": "^7.4", "laravel/framework": "^8.0|^9.0|^10.0" }, From b924ec44051bb64e35be0307cd0ddf6ca6f6d06d Mon Sep 17 00:00:00 2001 From: Quentin Schmick Date: Wed, 3 Sep 2025 09:41:46 -0400 Subject: [PATCH 3/6] Added new logging of response and request headers --- .github/workflows/php-cs-fixer.yml | 2 +- .github/workflows/phpstan.yml | 2 +- README.md | 42 ++++++++++++++++++++++++++++++ src/Models/RequestLogBaseModel.php | 16 ++++++++++++ stubs/migration.stub | 8 +++++- 5 files changed, 67 insertions(+), 3 deletions(-) diff --git a/.github/workflows/php-cs-fixer.yml b/.github/workflows/php-cs-fixer.yml index f55d1fa..2b72f27 100644 --- a/.github/workflows/php-cs-fixer.yml +++ b/.github/workflows/php-cs-fixer.yml @@ -8,7 +8,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 with: ref: ${{ github.head_ref }} diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index ebc58e3..daf6b43 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -14,7 +14,7 @@ jobs: steps: - name: "Checkout" - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: PHPStan uses: docker://oskarstark/phpstan-ga diff --git a/README.md b/README.md index 92282dd..5923676 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,19 @@ You can install the package via composer: composer require always-open/laravel-request-logger ``` +### Breaking change +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. +- Create a migration for each logging tables with the following +```php +// The headers that were part of the request +$table->json('request_headers') + ->nullable(); +// The headers that were part of the response +$table->json('response_headers') + ->nullable(); +``` +Run this migration prior to upgrading the package. + ## Configuration ``` php @@ -74,6 +87,35 @@ function makeFacebookApiCall(array $body, Client $facebook_client) $request_log->save(); } ``` + +Instead of manually setting the response data you can instead leverage the `updateFromRequest` method: +```php +function makeFacebookApiCall(array $body, Client $facebook_client) +{ + $request_headers = [ + 'api-key' => $config->apiKey, + 'Content-Type' => 'application/json', + ]; + + $versioned_path = self::buildVersionedUrlPath($path); + + $encoded_body = json_encode($body, JSON_UNESCAPED_SLASHES); + + $request = new Request( + 'GET', + '/v1/users', + $request_headers, + $encoded_body, + ); + + $request_log = FacebookRequestLog::makeFromGuzzle($request); + + $response = $client->send($request); + $request_log->updateFromResponse($response); +} +``` + + You can also manually set each property and then save the log instance. ### Testing diff --git a/src/Models/RequestLogBaseModel.php b/src/Models/RequestLogBaseModel.php index 3ff78f1..3a84475 100644 --- a/src/Models/RequestLogBaseModel.php +++ b/src/Models/RequestLogBaseModel.php @@ -5,6 +5,7 @@ use AlwaysOpen\RequestLogger\Observers\RequestLogObserver; use GuzzleHttp\Psr7\Request; use Illuminate\Database\Eloquent\Model; +use Psr\Http\Message\ResponseInterface; /** * AlwaysOpen\RequestLogger\Models\RequestLogBaseModel @@ -14,7 +15,9 @@ * @property string $http_method * @property int|null $response_code * @property array|string|null $body + * @property array|string|null $request_headers * @property array|string|null $response + * @property array|string|null $response_headers * @property string|null $exception * @property \Carbon\Carbon|null $occurred_at */ @@ -25,7 +28,9 @@ class RequestLogBaseModel extends Model 'created_at' => 'datetime', 'updated_at' => 'datetime', 'body' => 'json', + 'request_headers' => 'json', 'response' => 'json', + 'response_headers' => 'json', ]; protected $guarded = [ @@ -56,7 +61,18 @@ public static function makeFromGuzzle(Request $request) : static $instance->path = $request->getUri()->getPath(); $instance->http_method = $request->getMethod(); $instance->body = $request->getBody()->getContents(); + $instance->request_headers = $request->getHeaders(); return $instance; } + + public function updateFromResponse(ResponseInterface $response): self { + $this->response = json_decode($response->getBody()->getContents(), true); + $this->response_code = $response->getStatusCode(); + $this->response_headers = $response->getHeaders(); + + $this->save(); + + return $this; + } } diff --git a/stubs/migration.stub b/stubs/migration.stub index 6d89586..36ada0b 100644 --- a/stubs/migration.stub +++ b/stubs/migration.stub @@ -19,7 +19,7 @@ class {CLASS_NAME} extends Migration $table->string('path', 191) ->index(); // What parameters were passed in (e.g. ?status=new) - $table->string('params') + $table->string('params', 512) ->nullable() ->fulltext(); // HTTP method (e.g. POST/PUT/DELETE) @@ -32,9 +32,15 @@ class {CLASS_NAME} extends Migration // The entire JSON encoded payload of the request $table->json('body') ->nullable(); + // The headers that were part of the request + $table->json('request_headers') + ->nullable(); // The entire JSON encoded responses $table->json('response') ->nullable(); + // The headers that were part of the response + $table->json('response_headers') + ->nullable(); // Internal exceptions that occurred during the request $table->string('exception') ->nullable(); From fa12a44c711656f6a2550e8b81c9bd974597324c Mon Sep 17 00:00:00 2001 From: Quentin Schmick Date: Wed, 3 Sep 2025 10:06:32 -0400 Subject: [PATCH 4/6] Bumping deps --- composer.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index c075586..09f7a55 100644 --- a/composer.json +++ b/composer.json @@ -24,19 +24,19 @@ ], "require": { "php": "^8.2.0|^8.3.0|^8.4.0", - "always-open/laravel-process-stamps": "^5.0|^6.0|^7.0|^8.0", + "always-open/laravel-process-stamps": "^7.0|^8.0", "guzzlehttp/guzzle": "^7.4", - "laravel/framework": "^8.0|^9.0|^10.0" + "laravel/framework": "^11.0|^12.0" }, "require-dev": { - "doctrine/dbal": "^2.9|^3.0", + "doctrine/dbal": "^3.0|^4.0", "friendsofphp/php-cs-fixer": "^3.1", "laravel/tinker": "^2.7", - "nunomaduro/larastan": "^1.0", - "orchestra/testbench": "^6.22", - "phpstan/phpstan-deprecation-rules": "^1.0", - "phpstan/phpstan-phpunit": "^1.0", - "phpunit/phpunit": "^9.5" + "nunomaduro/larastan": "^3.0", + "orchestra/testbench": "^9.1", + "phpstan/phpstan-deprecation-rules": "^2.0", + "phpstan/phpstan-phpunit": "^2.0", + "phpunit/phpunit": "^12.0" }, "autoload": { "psr-4": { From a8e94ecca3b9eaae171f9a1a93796f5a38ffb6da Mon Sep 17 00:00:00 2001 From: qschmick Date: Wed, 3 Sep 2025 14:06:51 +0000 Subject: [PATCH 5/6] Fix styling --- src/Models/RequestLogBaseModel.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Models/RequestLogBaseModel.php b/src/Models/RequestLogBaseModel.php index 3a84475..65d5fc3 100644 --- a/src/Models/RequestLogBaseModel.php +++ b/src/Models/RequestLogBaseModel.php @@ -66,7 +66,8 @@ public static function makeFromGuzzle(Request $request) : static return $instance; } - public function updateFromResponse(ResponseInterface $response): self { + public function updateFromResponse(ResponseInterface $response): self + { $this->response = json_decode($response->getBody()->getContents(), true); $this->response_code = $response->getStatusCode(); $this->response_headers = $response->getHeaders(); From 4101cb0cb00069f0ad09bd4460fd85c8abbff397 Mon Sep 17 00:00:00 2001 From: Quentin Schmick Date: Wed, 3 Sep 2025 10:26:50 -0400 Subject: [PATCH 6/6] Update README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5923676..97f959d 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ function makeFacebookApiCall(array $body, Client $facebook_client) } ``` -Instead of manually setting the response data you can instead leverage the `updateFromRequest` method: +Instead of manually setting the response data you can instead leverage the `updateFromResponse` method: ```php function makeFacebookApiCall(array $body, Client $facebook_client) {