spatie/laravel-flare
Send Laravel 11+ production errors to Flare with a valid API key. Track exceptions, get notified when issues happen, and share error reports publicly when needed. Works on PHP 8.2+ and integrates seamlessly with your app’s reporting.
Installation:
composer require spatie/laravel-flare
Publish the config file:
php artisan vendor:publish --provider="Spatie\Flare\FlareServiceProvider" --tag="config"
Configuration:
Add your Flare API key to .env:
FLARE_API_KEY=your_api_key_here
Ensure APP_ENV is set to production (Flare only works in production).
First Use Case:
Trigger an error in production (e.g., 1/0 in a route or controller). Flare will automatically capture and display the error in your Flare dashboard.
'flare' to LOG_STACK in config/logging.php to send logs to Flare.minimal_log_level in config/flare.php (e.g., debug, info, warning).RateSampler or DynamicSampler to control which requests/logs are sent (e.g., sample 10% of /admin/* routes).abort(500) in a route).Flare::report($exception) in custom exception handlers or middleware.
use Spatie\Flare\Flare;
try {
// Risky code
} catch (\Exception $e) {
Flare::report($e);
throw $e; // Re-throw to ensure Laravel's error handling runs
}
// config/logging.php
'channels' => [
'flare' => [
'driver' => 'flare',
'minimal_log_level' => env('FLARE_LOG_LEVEL', 'debug'),
],
],
\Log::debug('User logged in', ['user_id' => 123]);
Flare will display logs with timestamps, levels, and context.// config/flare.php
'sampler' => \Spatie\Flare\Samplers\DynamicSampler::class,
'sampler_config' => [
'rules' => [
['route_name' => 'admin.*', 'sample_rate' => 0.1], // Sample 10% of admin routes
['queue_name' => 'high-priority', 'sample_rate' => 1.0], // Sample all high-priority jobs
],
],
// config/flare.php
'collects' => [
'custom' => function () {
return [
'app_version' => \Spatie\Flare\Flare::appVersion(),
'feature_flags' => \App\Services\FeatureFlags::enabled(),
];
},
],
// config/flare.php
'request_attribute_provider' => \App\Providers\FlareRequestAttributes::class,
'console_attribute_provider' => \App\Providers\FlareConsoleAttributes::class,
public function handle($request, Closure $next)
{
try {
return $next($request);
} catch (\Exception $e) {
if ($request->is('api/v1/*')) {
Flare::report($e);
}
throw $e;
}
}
DynamicSampler to control sampling:
'sampler_config' => [
'rules' => [
['queue_name' => 'notifications', 'sample_rate' => 0.5],
],
],
config/flare.php:
'database' => [
'enabled' => true,
'query_logging' => true,
],
.env to toggle Flare:
FLARE_ENABLED=false # Disable in staging
API Key Leaks:
.env to version control. Use environment-specific keys..env to .gitignore and use php artisan config:clear after key changes.Performance Overhead:
sample_rate (e.g., 1.0) can slow down production.0.1 and adjust based on monitoring.Sensitive Data Exposure:
config/flare.php:
'censor' => [
'headers' => ['authorization', 'cookie'],
'query' => ['password', 'token'],
'body' => ['credit_card'],
],
Queue Job Sampling:
queue:work --sync may not be sampled correctly.DynamicSampler with queue_connection rules:
'sampler_config' => [
'rules' => [
['queue_connection' => 'database', 'sample_rate' => 0.5],
],
],
Livewire Debugging:
APP_DEBUG=false in production (Flare handles debugging).Log Level Filtering:
minimal_log_level are dropped before leaving the app.minimal_log_level to debug for development, warning for production.Flare Daemon:
DaemonSender routes data through a local Flare daemon (useful for air-gapped environments).'sender' => \Spatie\Flare\Senders\DaemonSender::class,
Verify Installation:
php artisan flare:status
Flare is active.Test Locally:
FLARE_API_KEY=test_key to simulate production behavior without real API calls.Check Config:
config/flare.php for typos or missing keys (e.g., sampler_config).php artisan config:clear
Inspect Payloads:
FLARE_DEBUG=true to log raw payloads to storage/logs/flare.log.Common Errors:
Class not found: Ensure spatie/flare-client-php is installed and compatible.Missing API key: Verify .env and config/flare.php.No data in Flare: Check APP_ENV=production and network connectivity.Custom Collectors:
Spatie\Flare\Collectors\Collector to add app-specific data:
namespace App\FlareCollectors;
use Spatie\Flare\Collectors\Collector;
class UserCollector extends Collector
{
public function collect(): array
{
return [
'current_user' => auth()->user()?->id,
];
}
}
config/flare.php:
'collectors' => [
\App\FlareCollectors\UserCollector::class,
],
Override Samplers:
namespace App\Samplers;
use Spatie\Fl
How can I help you explore Laravel packages today?