Search by

unwahas / error-managements

liar88828

Error logging and protected error-log API for Laravel applications

v1.0.4 2026-11-23 02:54 UTC

This package is auto-updated.

Last update: 2026-11-23 04:43:53 UTC


README

A Laravel package for recording application exceptions and custom error messages, deduplicating repeat exceptions, and exposing a protected JSON API to browse, resolve, and delete logs.

Requirements

  • PHP 8.1+
  • Laravel 10, 11, 12, or 13

Installation

Install the package with Composer (composer require unwahas/error-managements). Laravel package discovery registers Unwahas\ErrorRedirect\ErrorRedirectServiceProvider automatically. Publish its configuration if you want to customize it:

php artisan vendor:publish --tag=error-redirect-config
php artisan migrate

The package registers its migration automatically. Configure the API key in .env before making requests:

ERROR_REDIRECT_API_KEY=replace-with-a-long-random-secret
ERROR_REDIRECT_REPORT_EXCEPTIONS=true

The routes are enabled by default at /api/error-logs. Every route requires the configured key in the X-Error-Log-Key header. Set ERROR_REDIRECT_API_ENABLED=false to disable the routes, or configure the route prefix, middleware, key header, and storage table in config/error-redirect.php.

Recording errors

use Unwahas\ErrorRedirect\Support\ErrorLog;

try {
    // Application operation...
} catch (Throwable $exception) {
    ErrorLog::record($exception, ['order_id' => $orderId]);
}

ErrorLog::message('Payment provider timed out', ['order_id' => $orderId]);

record() deduplicates by exception class, file, and line by default, incrementing the existing row's count. Override this for a single call with the third argument:

ErrorLog::record($exception, ['source' => 'webhook'], deduplicate: false);

Set ERROR_REDIRECT_REPORT_EXCEPTIONS=true to automatically record exceptions passed through Laravel's exception reporter. This is disabled by default to avoid changing existing reporting behavior unexpectedly.

Generate a secure code for ERROR_REDIRECT_API_KEY with the package's Artisan command:

php artisan error-redirect:generate-code

The command prints a 64-character code by default. Set a custom length between 16 and 128 characters with --length, then copy the output into your .env file as ERROR_REDIRECT_API_KEY.

The Eloquent model is Unwahas\ErrorRedirect\Models\ErrorLog.

Firebase Cloud Messaging (FCM) notifications

You can optionally send new error logs to Firebase Cloud Messaging (HTTP v1 API) by configuring your Firebase Service Account credentials in .env:

ERROR_REDIRECT_FIREBASE_ENABLED=true
ERROR_REDIRECT_FIREBASE_CREDENTIALS=/path/to/firebase-service-account.json
ERROR_REDIRECT_FIREBASE_PROJECT_ID=your-firebase-project-id
ERROR_REDIRECT_FIREBASE_TOPIC=error-logs
# Optional: target a specific device registration token instead of a topic
# ERROR_REDIRECT_FIREBASE_TOKEN=device-fcm-token
# Optional: HTTP request timeout in seconds (default: 5)
# ERROR_REDIRECT_FIREBASE_TIMEOUT=5
  • ERROR_REDIRECT_FIREBASE_CREDENTIALS accepts either a path to the Firebase Service Account JSON file (absolute or relative to the Laravel project root) or the raw JSON string itself.
  • ERROR_REDIRECT_FIREBASE_PROJECT_ID is optional if project_id is already included in the service account JSON.
  • Notifications are only sent when a new error log is created (deduplicated repeat occurrences increment count without re-sending).
  • Any Firebase network or authentication failures are safely swallowed so application exception reporting is never disrupted.

API endpoints

All endpoints use the configured route prefix and require the API key header.

Method Path Purpose
GET / Paginated logs; optional per_page (capped by configuration)
GET /new?after_id={id} Up to 20 newer logs
GET /latest-id Latest log ID
GET /notifications/count Unresolved count and newest unresolved log
GET /{id} A log and its stored context/trace
PATCH /{id}/resolve Toggle the resolved state
DELETE /{id} Delete a log

When using the default prefix /api/error-logs, for example, send X-Error-Log-Key: <configured key> with requests. API endpoints return JSON; deletion returns HTTP 204.

Local development

This repository is itself a Composer package. Run composer test to execute the Testbench feature suite, or composer validate to validate its package metadata. To use it from a Laravel application during development, add it as a Composer path repository and require unwahas/error-redirect.