Last active
December 16, 2023 21:22
-
-
Save aniket-magadum/23d91888bd4d1071280d11562d3884d7 to your computer and use it in GitHub Desktop.
Laravel Logging Http Client Request and Response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Support\Facades\Schema; | |
return new class extends Migration | |
{ | |
/** | |
* Run the migrations. | |
*/ | |
public function up(): void | |
{ | |
Schema::create('http_logs', function (Blueprint $table) { | |
$table->id(); | |
$table->string('url'); | |
$table->string('method'); | |
$table->smallInteger('status_code'); | |
$table->text('request_body'); | |
$table->text('response_body'); | |
$table->text('request_headers'); | |
$table->text('response_headers'); | |
$table->decimal('response_time',5,2); | |
$table->timestamps(); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
*/ | |
public function down(): void | |
{ | |
Schema::dropIfExists('http_logs'); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use App\Models\HttpLog; | |
use Illuminate\Foundation\Console\AboutCommand; | |
use Illuminate\Http\Client\Events\ResponseReceived; | |
use Illuminate\Support\ServiceProvider; | |
use Event; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Register any application services. | |
*/ | |
public function register(): void | |
{ | |
} | |
/** | |
* Bootstrap any application services. | |
*/ | |
public function boot(): void | |
{ | |
Event::listen(ResponseReceived::class, function (ResponseReceived $event) { | |
HttpLog::create([ | |
'url' => $event->request->url(), | |
'method' => $event->request->method(), | |
'status_code' => $event->response->status(), | |
'request_body' => $event->request->body(), | |
'response_body' => $event->response->body(), | |
'request_headers' => json_encode($event->request->headers()), | |
'response_headers' => json_encode($event->response->headers()), | |
'response_time'=> $event->response->transferStats->getHandlerStats()['total_time'] | |
]); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Factories\HasFactory; | |
use Illuminate\Database\Eloquent\Model; | |
class HttpLog extends Model | |
{ | |
use HasFactory; | |
protected $fillable = [ | |
'url', | |
'method', | |
'status_code', | |
'request_body', | |
'response_body', | |
'request_headers', | |
'response_headers', | |
'response_time' | |
]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Route; | |
/* | |
|-------------------------------------------------------------------------- | |
| Web Routes | |
|-------------------------------------------------------------------------- | |
| | |
| Here is where you can register web routes for your application. These | |
| routes are loaded by the RouteServiceProvider and all of them will | |
| be assigned to the "web" middleware group. Make something great! | |
| | |
*/ | |
Route::get('/', function () { | |
// Here we are triggering the actual API call for testing | |
Http::get('https://jsonplaceholder.typicode.com/todos'); | |
return view('welcome'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks