philiprehberger / php-exception-reporter
Lightweight exception reporting to log channels and webhooks
Package info
github.com/philiprehberger/php-exception-reporter
pkg:composer/philiprehberger/php-exception-reporter
v1.0.2
2026-03-17 20:06 UTC
Requires
- php: ^8.2
Requires (Dev)
- laravel/pint: ^1.0
- phpstan/phpstan: ^1.12|^2.0
- phpunit/phpunit: ^11.0
Suggests
- psr/log: For PSR-3 log channel integration (^3.0)
README
Lightweight exception reporting to log channels and webhooks.
Requirements
- PHP ^8.2
Installation
composer require philiprehberger/php-exception-reporter
Usage
Basic reporting with a callback
use PhilipRehberger\ExceptionReporter\ExceptionReporter; use PhilipRehberger\ExceptionReporter\Channels\CallbackChannel; $reporter = new ExceptionReporter(); $reporter->addChannel(new CallbackChannel(function ($report) { error_log("[{$report->class}] {$report->message} in {$report->file}:{$report->line}"); })); try { riskyOperation(); } catch (\Throwable $e) { $reporter->capture($e); }
File channel
use PhilipRehberger\ExceptionReporter\Channels\FileChannel; $reporter->addChannel(new FileChannel('/var/log/app-exceptions.log')); // Each report is written as a JSON line $reporter->capture(new \RuntimeException('Something failed'));
Multiple channels
$reporter ->addChannel(new CallbackChannel(function ($report) { // Send to your monitoring service })) ->addChannel(new FileChannel('/var/log/exceptions.log'));
Deduplication
Prevent the same exception (same class, file, and line) from being reported more than once:
$reporter->enableDeduplication(); $exception = new \RuntimeException('flaky'); $reporter->capture($exception); // Reported $reporter->capture($exception); // Skipped (duplicate) $reporter->resetFingerprints(); // Clear dedup state $reporter->capture($exception); // Reported again
Adding context
$reporter->capture($exception, [ 'user_id' => 42, 'request_url' => '/checkout', ]);
Custom channels
Implement the ReportChannel interface to build your own channel:
use PhilipRehberger\ExceptionReporter\Contracts\ReportChannel; use PhilipRehberger\ExceptionReporter\ExceptionReport; class SlackChannel implements ReportChannel { public function report(ExceptionReport $report): void { // POST to Slack webhook with $report->toArray() } }
API
ExceptionReporter
| Method | Description |
|---|---|
addChannel(ReportChannel $channel): self |
Register a reporting channel |
enableDeduplication(): self |
Enable fingerprint-based deduplication |
capture(Throwable $e, array $context = []): ExceptionReport |
Capture and report an exception |
resetFingerprints(): void |
Clear deduplication state |
ExceptionReport
| Property / Method | Description |
|---|---|
string $class |
Exception class name |
string $message |
Exception message |
string $file |
File where the exception was thrown |
int $line |
Line number |
string $trace |
Stack trace as string |
DateTimeImmutable $timestamp |
When the exception was captured |
array $context |
Additional context data |
?string $previousClass |
Previous exception class, if any |
?string $previousMessage |
Previous exception message, if any |
fingerprint(): string |
MD5 hash of class + file + line |
toArray(): array |
Serialize to array |
fromThrowable(Throwable, array): self |
Create from a throwable |
Channels
| Channel | Description |
|---|---|
CallbackChannel |
Invokes a user-provided callable |
FileChannel |
Appends JSON-encoded reports to a file |
Development
composer install vendor/bin/phpunit vendor/bin/pint --test vendor/bin/phpstan analyse
License
MIT