Skip to content

Commit 765c32a

Browse files
authored
Adding request logging (#7)
* Adds a migration to the published files for a logger class using the package always-open/laravel-request-logger * Refactored requests into a single method to simplify logging * Added new config option to disable/enable logging; enabled by defualt
1 parent fc6814b commit 765c32a

21 files changed

+1871
-160
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
],
2020
"require": {
2121
"php": "~8.3.1|~8.4.1",
22+
"always-open/laravel-request-logger": "^3.0",
2223
"illuminate/contracts": "~10.0||~11.0||~12.0",
2324
"spatie/laravel-data": "~4.16",
2425
"spatie/laravel-package-tools": "^1.16"
@@ -74,4 +75,4 @@
7475
},
7576
"minimum-stability": "dev",
7677
"prefer-stable": true
77-
}
78+
}

config/oxylabs-api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
'username' => env('OXYLAB_API_USERNAME'),
66
'password' => env('OXYLAB_API_PASSWORD'),
77
'auth_method' => env('OXYLAB_API_AUTH_METHOD', 'basic'),
8+
'request_logging_enabled' => env('OXYLABS_API_REQUEST_LOGGING_ENABLED', true),
89
];

config/request-logger.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
return [
4+
/*
5+
* This is the default suffix applied to a Model's table name.
6+
*/
7+
'table_suffix' => '_request_logs',
8+
9+
/*
10+
* This is the default suffix applied to models' class names.
11+
*
12+
* Example: The facebook object name would have a request log model of FacebookRequestLog.
13+
*/
14+
'model_suffix' => 'RequestLog',
15+
16+
'model_path' => app_path('Models'),
17+
18+
'model_stub' => __DIR__.'/../stubs/model.stub',
19+
20+
'migration_path' => database_path('migrations'),
21+
22+
'migration_stub' => __DIR__.'/../stubs/migration.stub',
23+
24+
/*
25+
* Enable the process stamps (sub) package to log which process/url/job invoked the call
26+
*/
27+
'enable_process_stamps' => true,
28+
29+
/*
30+
* Precision value used in generating occurred_at for request log
31+
*/
32+
'log_timestamp_precision' => 3,
33+
];
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
public function up(): void
10+
{
11+
Schema::create('oxylabs_api_request_logger', function (Blueprint $table) {
12+
$table->id();
13+
// Will store the relative path of the request (e.g. /addresses/validate)
14+
$table->string('path', 191)
15+
->index();
16+
// What parameters were passed in (e.g. ?status=new)
17+
$table->string('params', 512)
18+
->nullable()
19+
->fulltext();
20+
// HTTP method (e.g. POST/PUT/DELETE)
21+
$table->string('http_method', 10)
22+
->index();
23+
// Status code (e.g. 200, 204, 429)
24+
$table->smallInteger('response_code', autoIncrement: false, unsigned: true)
25+
->nullable()
26+
->index();
27+
// The entire JSON encoded payload of the request
28+
$table->json('body')
29+
->nullable();
30+
// The headers that were part of the request
31+
$table->json('request_headers')
32+
->nullable();
33+
// The entire JSON encoded responses
34+
$table->json('response')
35+
->nullable();
36+
// The headers that were part of the response
37+
$table->json('response_headers')
38+
->nullable();
39+
// Internal exceptions that occurred during the request
40+
$table->string('exception')
41+
->nullable();
42+
// When the request was resolved to the millisecond
43+
$table->timestamp('occurred_at', 3)->index();
44+
$table->timestamps(precision: 3);
45+
$table->processIds();
46+
});
47+
}
48+
49+
public function down(): void
50+
{
51+
Schema::dropIfExists('oxylabs_api_request_logger');
52+
}
53+
};

database/migrations/create_oxylabs_api_table.php.stub

Lines changed: 0 additions & 19 deletions
This file was deleted.

phpstan.neon.dist

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ parameters:
1010
checkOctaneCompatibility: true
1111
checkModelProperties: true
1212
ignoreErrors:
13-
- '#Trait AlwaysOpen\\OxylabsApi\\Traits\\APIStatus is used zero times and is not analysed.#'
14-
- '#Call to function is_string\(\) with string will always evaluate to true.#'
13+
- '#Trait AlwaysOpen\\OxylabsApi\\Traits\\APIStatus is used zero times and is not analysed.#'

phpunit.xml

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd" bootstrap="vendor/autoload.php"
4-
executionOrder="depends,defects" beStrictAboutOutputDuringTests="true" failOnRisky="false"
5-
failOnWarning="false" cacheDirectory=".phpunit.cache" requireCoverageMetadata="true"
6-
beStrictAboutCoverageMetadata="true">
3+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
cacheDirectory=".phpunit.cache"
6+
executionOrder="depends,defects"
7+
shortenArraysForExportThreshold="10"
8+
requireCoverageMetadata="false"
9+
beStrictAboutCoverageMetadata="true"
10+
beStrictAboutOutputDuringTests="true"
11+
displayDetailsOnPhpunitDeprecations="true"
12+
failOnPhpunitDeprecation="true"
13+
failOnRisky="true"
14+
failOnWarning="true">
715
<testsuites>
816
<testsuite name="default">
9-
<directory suffix=".php">tests</directory>
17+
<directory>tests</directory>
1018
</testsuite>
1119
</testsuites>
12-
<coverage>
13-
<report>
14-
<html outputDirectory="reports/coverage/html" lowUpperBound="50" highLowerBound="90"/>
15-
<clover outputFile="build/logs/clover.xml"/>
16-
</report>
17-
</coverage>
18-
<source>
20+
21+
<source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true">
1922
<include>
20-
<directory suffix=".php">src</directory>
23+
<directory>src</directory>
2124
</include>
2225
</source>
26+
<php>
27+
<server name="OXYLABS_API_REQUEST_LOGGING_ENABLED" value="false" force="true"/>
28+
<env name="OXYLABS_API_REQUEST_LOGGING_ENABLED" value="false" force="true"/>
29+
</php>
2330
</phpunit>

phpunit.xml.dist

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
5-
backupGlobals="false"
6-
bootstrap="vendor/autoload.php"
7-
colors="true"
8-
processIsolation="false"
9-
stopOnFailure="false"
10-
executionOrder="random"
11-
failOnWarning="true"
12-
failOnRisky="true"
13-
failOnEmptyTestSuite="true"
14-
beStrictAboutOutputDuringTests="true"
15-
cacheDirectory=".phpunit.cache"
16-
backupStaticProperties="false"
17-
>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
cacheDirectory=".phpunit.cache"
6+
executionOrder="depends,defects"
7+
shortenArraysForExportThreshold="10"
8+
requireCoverageMetadata="true"
9+
beStrictAboutCoverageMetadata="true"
10+
beStrictAboutOutputDuringTests="true"
11+
displayDetailsOnPhpunitDeprecations="true"
12+
failOnPhpunitDeprecation="true"
13+
failOnRisky="true"
14+
failOnWarning="true">
1815
<testsuites>
19-
<testsuite name="VendorName Test Suite">
16+
<testsuite name="default">
2017
<directory>tests</directory>
2118
</testsuite>
2219
</testsuites>
2320
<logging>
2421
<junit outputFile="build/report.junit.xml"/>
2522
</logging>
26-
<source>
23+
24+
<source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true">
2725
<include>
28-
<directory suffix=".php">./src</directory>
26+
<directory>src</directory>
2927
</include>
3028
</source>
29+
<php>
30+
<server name="OXYLABS_API_REQUEST_LOGGING_ENABLED" value="false" force="true"/>
31+
<env name="OXYLABS_API_REQUEST_LOGGING_ENABLED" value="false" force="true"/>
32+
</php>
3133
</phpunit>

src/DTOs/Amazon/AmazonSellerResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class AmazonSellerResponse extends Data
1414
public function __construct(
1515
/* @var AmazonSellerResult[] $results */
1616
#[DataCollectionOf(AmazonSellerResult::class)]
17-
public readonly array $results,
18-
public readonly PushPullJob $job,
17+
public readonly ?array $results,
18+
public readonly ?PushPullJob $job,
1919
) {}
2020
}

src/DTOs/Google/GoogleShoppingProductResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class GoogleShoppingProductResponse extends Data
1515
public function __construct(
1616
/* @var GoogleShoppingProductResult[] $results */
1717
#[DataCollectionOf(GoogleShoppingProductResult::class)]
18-
public readonly DataCollection $results,
19-
public readonly PushPullJob $job,
18+
public readonly ?DataCollection $results,
19+
public readonly ?PushPullJob $job,
2020
) {}
2121
}

0 commit comments

Comments
 (0)