Laravel Spy

Laravel Spy stats

Downloads
79
Stars
19
Open Issues
0
Forks
1

View on GitHub →

A Laravel package to track outgoing HTTP requests.

Laravel Spy

Laravel Spy is a lightweight Laravel package designed to track and log outgoing HTTP requests made by your Laravel application.

This package is useful for debugging, monitoring, and auditing external API calls or HTTP requests, providing developers with a zero config, simple way to inspect request details such as URLs, methods, headers, and responses.

Features

  • Tracks all outgoing HTTP requests made via Laravel's HTTP client.
  • Logs request details, including URL, method, headers, payload, and response.
  • Configurable logging options to customize and obfuscate sensitive data.

Requirements

  • PHP: ^8.1
  • Laravel: ^10.0 | ^11.0 | ^12.0
  • Development Dependencies (optional):
    • laravel/pint: ^1.0 (for code style linting)
    • phpunit/phpunit: ^9.0 (for running tests)

Installation

You can install the package via Composer:

composer require farayaz/laravel-spy

The package uses Laravel's auto-discovery feature. After installation, the package is ready to use with its default configuration.

By Default Laravel-Spy logs all HTTP requests and API calls. You can customize it to exclude specific URLs or obfuscate sensitive data.

Configuration

To customize the behavior of Laravel Spy, you can publish the configuration file:

php artisan vendor:publish --provider="Farayaz\LaravelSpy\LaravelSpyServiceProvider"
php artisan migrate

This will create a config/spy.php file where you can configure the following options:

'table_name' => 'http_logs',
 
'enabled' => env('SPY_ENABLED', true),
'db_connection' => env('SPY_DB_CONNECTION'),
 
'exclude_urls' => explode(',', env('SPY_EXCLUDE_URLS', '')),
'obfuscates' => explode(',', env('SPY_OBFUSCATES', 'password')),

Usage

Once installed and configured, Laravel Spy automatically tracks all outgoing HTTP requests made using Laravel's Http facade or HTTP client. The package logs the following details for each request:

  • 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

Example:

Once you’ve installed Laravel-Spy via Composer and published the configuration, open your web.php file and add the following line to start logging results into the http_logs table in your database:

Route::get("/spy", function () {
Http::get('https://github.com/farayaz/laravel-spy/');
});

Now head to the http_logs table to view the logged parameters.

Cleaning up logs

Laravel Spy provides a spy:clean command to remove old HTTP logs:

# Clean logs based on your config
php artisan spy:clean
 
# Clean logs older than 30 days
php artisan spy:clean --days=30
 
# Clean logs matching URL pattern
php artisan spy:clean --days=1 --url=api/users

Automated cleanup

You can schedule automatic cleanup in your Laravel scheduler:

// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('spy:clean')->daily();
}

Contributing

Contributions are welcome! To contribute to Laravel Spy:

  • Fork the repository on GitHub.
  • Clone your fork and create a new branch (git checkout -b feat-your-feature).
  • Run code style checks with Laravel Pint (vendor/bin/pint).
  • Commit your changes and push to your fork.
  • Create a pull request with a clear description of your changes.

Issues

If you encounter any issues or have feature requests, please open an issue on the GitHub repository. Provide as much detail as possible, including:

  • Laravel version
  • PHP version
  • Package version
  • Steps to reproduce
  • Expected vs. actual behavior
  • Any relevant error messages or logs

License

Laravel Spy is open-sourced software licensed under the MIT License.

Contact

For questions or support, reach out via the GitHub repository or open an issue.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Farayaz Laravel Spy Related Articles

HTTP Client Request Attributes in Laravel 12.42 image

HTTP Client Request Attributes in Laravel 12.42

Read article
Log Outgoing HTTP Requests with the Laravel Spy Package image

Log Outgoing HTTP Requests with the Laravel Spy Package

Read article
Laravel SpaceOCR: Parse Images and Multi-page PDFs in Laravel image

Laravel SpaceOCR: Parse Images and Multi-page PDFs in Laravel

Read article
Conduct better email testing with Mail Intercept image

Conduct better email testing with Mail Intercept

Read article
Laravel 5.7.10 Released image

Laravel 5.7.10 Released

Read article
Statamic 2.9 is now released image

Statamic 2.9 is now released

Read article
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic
LoadForge logo

LoadForge

Scalable load testing for web apps & APIs. Simulate real-world traffic and identify breaking points and performance limits with powerful, scalable load tests designed for Laravel.

LoadForge
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum