spatie/flare-client-php
PHP 8.2+ client for sending exceptions, errors, and stack traces to Flare. Install via Composer and use in any PHP app; Laravel users should use spatie/laravel-flare. Includes docs, tests, and ongoing maintenance by Spatie.
Installation:
composer require spatie/flare-client-php
Ensure your PHP version is 8.2+.
Basic Initialization:
use Spatie\FlareClient\Flare;
$flare = Flare::make('YOUR_FLARE_API_KEY');
First Use Case: Capture an exception:
try {
// Risky code
} catch (\Exception $e) {
$flare->report($e);
}
For Laravel: Use the dedicated Laravel package instead, which integrates seamlessly with Laravel’s error handling.
$flare->report(new \RuntimeException('Oops!'));
$flare->reportError(new \Error('Division by zero'));
Use the dedicated logger for structured logs:
$flare->logger()->error('User login failed', ['user_id' => 123]);
Supports Monolog’s Level enum (e.g., debug(), info(), warning()).
$span = $flare->tracer()->startSpan('database_query');
$span->end();
$flare->tracer()->startSubtask('checkout_process');
Control trace collection with dynamic rules:
$flare->sampler()->addRule(
new \Spatie\FlareClient\Sampling\SamplingRule(
entryPoint: 'CheckoutController',
percentage: 100.0,
)
);
Customize data collection via providers (e.g., for requests, users):
$flare->setRequestAttributesProvider(
fn (\Symfony\Component\HttpFoundation\Request $request) => [
'custom_field' => $request->get('custom_data'),
]
);
FlareMiddleware to auto-capture HTTP errors.JobRecorder to trace queue workers:
$flare->jobRecorder()->record($job);
HttpKernel events for request/response tracing.$flare->tracer()->startSpan('cli_command')->end();
DaemonSender (bundled) for local buffering:
$flare->setSender(new \Spatie\FlareClient\Sender\DaemonSender());
Double Reporting:
$flare->report() in both try/catch blocks and global error handlers (e.g., set_exception_handler). Use Flare::make() once and reuse the instance.Sampling Conflicts:
Sensitive Data Leaks:
$flare->setRequestAttributesProvider(
fn ($request) => array_filter($request->request->all(), fn ($key) => !str_starts_with($key, 'password'))
);
$flare->censor('user.*', 'payment.*');
Trace Context:
$flare->disableTracing()) must be called before starting spans to avoid leaks.Deprecated Methods:
reportMessage() (replaced by $flare->logger()).SpanTrait, SpanEventTrait) are deprecated; use dedicated recorder classes.Local Testing:
flare:test command to simulate errors:
php artisan flare:test --error="Division by zero"
Sender Issues:
DaemonSender logs if Flare reports aren’t reaching the server. Fallback to CurlSender:
$flare->setSender(new \Spatie\FlareClient\Sender\CurlSender());
Sampling Debugging:
$flare->sampler()->shouldSample($entryPoint, $parentSampled);
Attribute Overrides:
Custom Recorders:
Extend Recorder to add domain-specific data (e.g., database queries):
class CustomRecorder implements \Spatie\FlareClient\Recorder\Recorder
{
public function record(\Spatie\FlareClient\EntryPoint $entryPoint): void
{
// Add custom spans/logs
}
}
$flare->addRecorder(new CustomRecorder());
Attribute Providers:
Implement interfaces like RequestAttributesProvider to inject app-specific metadata:
class AppUserProvider implements \Spatie\FlareClient\AttributeProvider\UserAttributesProvider
{
public function getAttributes(): array
{
return ['user_role' => auth()->user()->role];
}
}
$flare->setUserAttributesProvider(new AppUserProvider());
Lifecycle Hooks: Reset state between requests (e.g., in middleware):
$flare->lifecycle()->reset();
Grouping Overrides: Customize error grouping (e.g., by exception class + code):
$flare->setGroupingOverride(
\Spatie\FlareClient\Grouping\GroupingOverride::fullStacktraceAndExceptionClassAndCode()
);
How can I help you explore Laravel packages today?