philiprehberger/php-exception-reporter

Lightweight exception reporting to log channels and webhooks

Maintainers

Package info

github.com/philiprehberger/php-exception-reporter

pkg:composer/philiprehberger/php-exception-reporter

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

v1.0.2 2026-03-17 20:06 UTC

This package is auto-updated.

Last update: 2026-03-17 20:14:44 UTC


README

Tests Latest Version on Packagist License

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