These days, building a web app without using a third-party API is almost impossible!
One of the major things when making outgoing HTTP requests is to review the requests and responses to see what’s happening between our application and the third-party API.
Tracking and logging outgoing HTTP requests will help us improve observability, detect slow APIs, and monitor what’s wrong and why.
Another important thing is having a timeline of requests and responses, so we can review them and extract useful data that we might have missed during the API implementation.
Laravel Spy is a powerfull Laravel package designed to track and log outgoing HTTP requests made by your Laravel application.
Laravel Spy is useful for debugging, monitoring, and auditing external HTTP requests, providing an easy way to inspect details such as URLs, methods, headers, and responses.
To use this package, we first need to install it. So we run:
composer require farayaz/laravel-spy
After the installation, we publish the package configuration files:
php artisan vendor:publish --provider="Farayaz\LaravelSpy\LaravelSpyServiceProvider"
Then, run the migrations to prepare the database for storing request logs:
php artisan migrate
Now, If we go into config/spy.php
, the configuration file has these options:
'table_name' => 'http_logs', // table name for storing http logs
'enabled' => env('SPY_ENABLED', true), // is laravel spy enabled or not
'db_connection' => env('SPY_DB_CONNECTION'), // using another db connection for http logs
'exclude_urls' => explode(',', env('SPY_EXCLUDE_URLS', '')), // add url which don't need to be logged by laravel-spy
'obfuscates' => explode(',', env('SPY_OBFUSCATES', 'password')), // obfuscates and hide the sensitive data
Laravel Spy gives us an ability to use a separate database connection via the db_connection
in cofing file, so we don't abuse the main database connection too much in high-traffic applications.
Also we should be mindful about setting the obfuscates
option inside the config/spy.php
to hide sensitive data in our API logs like: password, secret and etc.
To check that everything is working we just need to call a sample API through our application. We can add the below code inside our web.php
or api.php
in our laravel application:
Route::get("/spy", function () {
Http::get('https://github.com/farayaz/laravel-spy/');
});
After calling the sample /spy
route, head to the http_logs
table to view the logged parameters in database.
Now for each request, We can easily see:
- The full URL of the request
- The HTTP method (e.g., GET, POST, PUT)
- Request Headers
- Request Body
- Response Header
- Response Body
- Response HTTP Status code
That’s it! Laravel Spy is open-source under the MIT License, and we would be more than grateful for any contributions to this package.
Top comments (2)
Did you ever looked at laravel telescope ? Logging is why Telescope exists (and much more). Perhaps he'll also track http calls.
I knew about it from day one of it's release. But it appears you didn’t understand the situation quite well.