Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/php-cs-fixer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: PHPStan
uses: docker://oskarstark/phpstan-ga
Expand Down
11 changes: 5 additions & 6 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 `updateFromResponse` 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
Expand Down
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@
}
],
"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": "^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": {
Expand Down
17 changes: 17 additions & 0 deletions src/Models/RequestLogBaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*/
Expand All @@ -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 = [
Expand Down Expand Up @@ -56,7 +61,19 @@ 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;
}
}
8 changes: 7 additions & 1 deletion stubs/migration.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
Expand Down