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.
## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require spatie/flare-client-php
For Laravel, prefer spatie/laravel-flare for seamless integration.
Configuration:
Add your Flare API key to .env:
FLARE_API_KEY=your_api_key_here
First Use Case: Capture an exception in a controller or service:
use Spatie\FlareClient\Flare;
try {
// Risky operation
} catch (\Exception $e) {
Flare::report($e); // Sends to Flare
}
Key Classes:
Flare: Main entry point for reporting errors, logs, and traces.Flare::report(): Send exceptions/errors.Flare::log(): Log messages with levels (e.g., Flare::log('Debug', 'Message')).Flare::tracer(): Start/manage traces (e.g., Flare::tracer()->startSpan('operation')).Where to Look First:
Flare class methods (autocomplete in IDE).try {
// Code that may fail
} catch (\Exception $e) {
Flare::report($e); // Auto-captures stack traces, context, etc.
}
Flare::report($e)
->withAttribute('user_id', auth()->id())
->withAttribute('custom_data', ['key' => 'value']);
$trace = Flare::tracer()->startTrace('user-purchase');
$span = $trace->startSpan('process-payment');
// Business logic...
$span->end();
$trace->end();
$parentTrace = Flare::tracer()->getCurrentTrace();
$childTrace = Flare::tracer()->startTrace('child-operation', parent: $parentTrace);
Flare::log('info', 'Order processed', [
'order_id' => 123,
'user_id' => auth()->id(),
]);
Level enum (Flare::log(Monolog\Level::Error, 'Message')).Flare::sampler()->addRule(
SamplingRule::create()
->forEntryPoint('checkout')
->sample(100) // 100% sampling for checkout flows
);
Flare::entryPointResolver()->addEntryPoint(
EntryPoint::route('checkout.store')
);
Flare::requestAttributesProvider(fn () => [
'custom_metric' => request()->ip(),
]);
RequestAttributesProviderRouteAttributesProviderJobAttributesProvider (for queues)UserAttributesProviderFlare::queueRecorder()->recordJob($job, $payload);
Flare::tracer()->startTrace('job', subtask: true);
Flare::lifecycle()->flush(); // Manually trigger sends
Flare::lifecycle()->reset(); // Clear in-memory state
App\Exceptions\Handler to auto-report:
public function report(Throwable $exception) {
if (! app()->bound('flare')) return;
app('flare')->report($exception);
}
FlareMiddleware (from laravel-flare) to auto-capture HTTP errors.kernel.exception to report errors:
$event->getThrowable(); // Report via Flare
php artisan flare:test
Or programmatically:
Flare::testTrace()->startTrace('test-operation');
DaemonSender by default. Ensure the Flare daemon is running for offline buffering.Sampling Misconfiguration:
SamplingRules cover your entry points:
Flare::sampler()->addRule(
SamplingRule::create()->sample(100) // Ensure this matches your use case
);
Flare::ids()->traceId to confirm traces are generated.Attribute Overrides:
Flare::requestAttributesProvider(fn () => ['key' => 'value']);
Flare::report($e); // Now includes the attribute
Subtask Mode Not Working:
subtask: true:
Flare::tracer()->startTrace('job', subtask: true);
Daemon Unreachable:
DaemonSender errors.Sensitive Data Leakage:
Flare::ignoreRequestBody(); // Globally
Flare::ignoreResponseBody(); // Globally
Or per-field:
Flare::ignoreRequestBodyFields(['password', 'credit_card']);
PHP 8.4+ Deprecations:
implictly nullable or non-scalar type errors.Grouping Overrides:
FullStacktraceAndExceptionClassAndCode:
Flare::report($e)->withGroupingOverride(
GroupingOverride::fullStacktraceAndExceptionClassAndCode()
);
Check Sent Reports:
dd(Flare::sentReports()); // Inspect before sending
Enable Verbose Logging:
Flare::logger()->setLevel(Monolog\Level::Debug);
Inspect Trace IDs:
$traceId = Flare::ids()->traceId;
// Use in logs for correlation
Disable Flare Temporarily:
Flare::disable(); // Skip all reporting
Validate API Key:
FLARE_API_KEY is correct and the key has permissions in Flare’s dashboard.Custom Recorders:
RecorderInterface to add new data sources (e.g., database queries):
Flare::addRecorder(new class implements RecorderInterface {
public function record(RecorderData $data) { /* ... */ }
});
Attribute Providers:
Flare::
How can I help you explore Laravel packages today?