saloonphp/barstool
Laravel package that automatically logs Saloon requests and responses to your database for easy searching and filtering. Captures method, URL, headers/body, status, duration, and even fatal errors, with optional config and model pruning support.
Installation
composer require saloonphp/barstool
Publish the config file:
php artisan vendor:publish --provider="Saloon\Barstool\BarstoolServiceProvider"
Configure Database
Ensure your .env has a database connection (e.g., DB_CONNECTION=mysql). Run migrations:
php artisan migrate
Note: If migrations fail due to incorrect config keys, verify config/barstool.php uses the correct database connection key (e.g., mysql instead of deprecated pgsql if applicable).
First Use Case: Logging a Request
Extend your Saloon Http class to use Barstool:
use Saloon\Barstool\Concerns\LogsRequests;
class MyApi extends Http
{
use LogsRequests;
}
Now, every request made via MyApi will be automatically logged to the database.
config/barstool.php – Adjust logging behavior (e.g., enabled/disabled, table names).database/migrations/ – Inspect barstool_requests and barstool_responses tables. Note: The status column (if present) has been removed in v1.1.1.app/Http/Middleware/ – Check if Barstool middleware is registered (if using HTTP logging).Automatic Logging
Use the LogsRequests trait in your Saloon Http classes to log all requests/responses:
class GitHubApi extends Http
{
use LogsRequests;
public function resolveEndpoint(): string
{
return 'https://api.github.com/user';
}
}
Manual Logging Log specific requests/responses manually:
$response = $this->send(new MyRequest());
Barstool::logRequest($this->endpoint(), $this->connector(), $response);
Querying Logs
Use the Barstool facade to fetch logs:
$logs = Barstool::getRequests(['limit' => 10]); // Returns a collection of logged requests
$failedLogs = Barstool::getRequests(['status_code' => 500]);
barstool_requests table via migrations to store additional metadata (e.g., user_id for tracking which user made the request).Conditional Logging Disable logging for specific endpoints:
class MyApi extends Http
{
use LogsRequests;
protected bool $shouldLogRequests = false; // Disable logging
}
Logging HTTP Requests
Use the LogsHttpRequests middleware to log HTTP requests (not just Saloon):
// In AppServiceProvider@boot()
$this->app->make(\Saloon\Barstool\Http\Middleware\LogsHttpRequests::class)->appendToKernel();
Exporting Logs Create a custom exporter to dump logs to a file or external service:
Barstool::exportLogsToFile('path/to/logs.json');
Performance Overhead
Barstool::queueLog($request, $response); // Uses Laravel queues
Table Bloat
barstool_requests and barstool_responses tables can grow large. Implement a cleanup job:
php artisan schedule:run
Add to app/Console/Kernel.php:
protected function schedule(Schedule $schedule)
{
$schedule->command('barstool:prune --days=30')->daily();
}
Sensitive Data
shouldLogBody() to filter payloads:
class MyApi extends Http
{
protected function shouldLogBody(): bool
{
return !in_array($this->endpoint(), ['/sensitive']);
}
}
Migration Issues
status column (if present) is removed from your database tables. Run fresh migrations if needed:
php artisan migrate:fresh --env=testing
Log Not Appearing?
LogsRequests trait is used in your Http class.barstool config is enabled ('enabled' => true in config/barstool.php)..env and matches the config key (e.g., mysql instead of deprecated keys).Querying Issues
Barstool::getRequests() with dd() to inspect the returned data structure:
dd(Barstool::getRequests(['limit' => 1]));
storage/logs/laravel.log).Middleware Conflicts
LogsHttpRequests, ensure it’s registered after Saloon’s middleware in app/Http/Kernel.php.Custom Logging Logic
Override the logRequest method in a service provider:
Barstool::extend(function ($request, $response) {
// Custom logic (e.g., add tags, enrich data)
$response->setMetadata(['custom_field' => 'value']);
});
Event Listeners
Listen to Barstool\Events\RequestLogged to react to logged requests:
Barstool::listen(function ($request) {
// Trigger analytics, notifications, etc.
});
Testing Mock Barstool in tests to avoid database hits:
Barstool::shouldReceive('logRequest')->once();
How can I help you explore Laravel packages today?