spatie/flare-client-php
PHP client for sending errors, exceptions, and stack traces to Flare. Install via Composer, configure your API key, and report incidents from any PHP 8.2+ app. For Laravel integrations, use spatie/laravel-flare.
Installation:
composer require spatie/flare-client-php
For Laravel projects, prefer spatie/laravel-flare instead.
Configuration:
Add your Flare API key to .env:
FLARE_API_KEY=your_api_key_here
First Use Case: Report an exception manually:
use Spatie\FlareClient\Flare;
try {
// Risky code
} catch (\Exception $e) {
Flare::report($e);
}
Automatic Error Handling:
Register the error handler in your bootstrap file (e.g., bootstrap/app.php):
Flare::registerErrorHandler();
src/Flare.php – Core client class.src/Report.php – Report structure and customization.src/Collectors/ – Attribute collectors (e.g., Git, Request, Console).// Basic report
Flare::report(new \RuntimeException('Oops!'));
// With custom context
Flare::report(new \RuntimeException('Oops!'))
->withContext(['user_id' => 123, 'action' => 'delete']);
$report = Flare::report(new \Exception('Custom report'))
->withGroupingOverride('FullStacktraceAndExceptionClassAndCode')
->withoutSensitiveData(['password', 'api_key'])
->withTraceContext(['custom_key' => 'value']);
Use a callback to filter reports before sending:
Flare::setReportFilter(function ($report) {
return !in_array($report->exception->getCode(), [404, 500]);
});
Extend or replace default collectors (e.g., for custom request data):
Flare::setRequestAttributeProvider(function ($request) {
return ['custom_field' => $request->input('custom_field')];
});
Record custom spans or events:
$span = Flare::startSpan('database_query');
try {
DB::query(...);
} finally {
$span->end();
}
spatie/laravel-flare for seamless Laravel integration (handles middleware, service providers, and more).set_exception_handler or set_error_handler.HttpKernel to use Flare’s error handler:
$kernel->getContainer()->set('flare.error_handler', Flare::errorHandler());
Flare::testTrace() to simulate errors in tests:
Flare::testTrace(function () {
throw new \RuntimeException('Test error');
});
Flare::setCensorRequestBody(true);
Flare::setCensorCookies(['session', 'auth_token']);
Double Reporting:
registerErrorHandler() and a custom set_exception_handler are used, errors may be reported twice. Disable the default handler if needed:
Flare::registerErrorHandler(false); // Disables default error handler
Sensitive Data Leaks:
Flare::setCensorRequestBodyFields(['user.password', '*.api_key']);
Performance Overhead:
Flare::disableGitCollector();
Grouping Conflicts:
FullStacktraceAndExceptionClassAndCode) may reduce Flare’s ability to group similar errors. Use sparingly.API Rate Limits:
setReportFilter() to throttle reports.Check API Responses:
Flare::setDebugMode(true);
flare.log file in your project root for API issues.Verify API Key:
FLARE_API_KEY is correctly set in .env and not empty.Network Issues:
https://api.flareapp.io.Stack Trace Issues:
FLARE_BASE_PATH is set to your project root:
Flare::setBasePath(__DIR__);
Custom Collectors:
Spatie\FlareClient\Collectors\CollectorInterface to add project-specific data:
class CustomCollector implements CollectorInterface {
public function collect(): array {
return ['custom_data' => 'value'];
}
}
Flare::addCollector(new CustomCollector());
Override Report Filtering:
Flare::setReportFilter(function ($report) {
return $report->exception->getCode() !== 404;
});
Modify API Sender:
Spatie\FlareClient\Senders\ApiSender to customize HTTP requests (e.g., add headers):
Flare::setApiSender(function () {
return new class extends \Spatie\FlareClient\Senders\ApiSender {
protected function getHeaders(): array {
return array_merge(parent::getHeaders(), ['X-Custom-Header' => 'value']);
}
};
});
Disable Tracing for Specific Routes:
Flare::disableTracing();
Environment Variables:
FLARE_API_KEY (required)FLARE_DEBUG (enable debug logs)FLARE_DISABLED (set to true to disable entirely)FLARE_BASE_PATH (override project root for stack traces)Laravel-Specific:
spatie/laravel-flare) auto-configures most settings. Avoid mixing it with spatie/flare-client-php unless necessary.PHP 8.5+:
declare(strict_types=1) to avoid deprecation warnings. The package is compatible with PHP 8.5 but may require type adjustments in custom code.Git Collector:
Flare::disableGitCollector();
How can I help you explore Laravel packages today?