adrianoalves/laravel-exceptionlog
Minimal Laravel 7+ package to persist exceptions to a database logs table. Install via Composer, migrate, then call ExceptionLog::persist($exception, $level). Includes simple level mapper (app, DB, server, console, jobs) and is customizable.
Installation:
composer require adrianoalves/laravel-exceptionlog
Publish the config file:
php artisan vendor:publish --provider="AdrianoAlves\ExceptionLog\ExceptionLogServiceProvider"
Configuration:
Edit config/exceptionlog.php to define:
driver (e.g., database, log, or slack)database driver)slack driver)log driver)First Use Case: Trigger an exception in a route or controller:
public function testException()
{
throw new \Exception("Test exception for logging");
}
Verify the exception is logged by checking:
exception_logs) for database driver.storage/logs/laravel.log) for log driver.slack driver.Exception Handling: Override Laravel’s default exception handler to log exceptions:
// app/Exceptions/Handler.php
public function report(Throwable $exception)
{
ExceptionLog::log($exception);
parent::report($exception);
}
Manual Logging: Log exceptions manually in custom logic:
try {
// Risky operation
} catch (\Exception $e) {
ExceptionLog::log($e, ['user_id' => auth()->id()]);
}
Custom Fields: Attach metadata to exceptions:
ExceptionLog::log($e, [
'request_id' => $request->header('X-Request-ID'),
'context' => 'payment_processing'
]);
Slack Notifications: Configure Slack alerts for critical errors:
// config/exceptionlog.php
'slack' => [
'webhook_url' => env('SLACK_WEBHOOK_URL'),
'level' => 'critical', // Only log 'critical' errors
]
Queue Exceptions: Use Laravel queues to defer logging for performance:
ExceptionLog::log($e)->onQueue('exception-logging');
Middleware for API Errors: Log API-specific exceptions in middleware:
public function handle($request, Closure $next)
{
try {
return $next($request);
} catch (\Exception $e) {
ExceptionLog::log($e, ['api_endpoint' => $request->path()]);
throw $e;
}
}
Database Schema Customization:
Extend the exception_logs table by creating a migration:
Schema::table('exception_logs', function (Blueprint $table) {
$table->string('custom_field')->nullable();
});
Missing Config:
Forgetting to publish the config file (php artisan vendor:publish) will result in default settings (e.g., no Slack integration).
Database Driver Issues:
Ensure the exception_logs table exists and is writable. Run migrations if needed:
php artisan migrate
Slack Rate Limits:
Excessive logging to Slack may hit rate limits. Use the level config to filter errors (e.g., critical only).
Performance Overhead: Logging every exception can slow down requests. Use queues or defer logging for non-critical errors.
Check Logs: Verify exceptions are being logged by inspecting:
SELECT * FROM exception_logs ORDER BY created_at DESC LIMIT 10;tail -f storage/logs/laravel.logEnable Debug Mode:
Temporarily enable debug mode in config/exceptionlog.php to log additional context:
'debug' => env('APP_DEBUG', false),
Custom Drivers:
Extend the package by creating a custom driver. Implement the AdrianoAlves\ExceptionLog\Contracts\Driver interface.
Event Listeners:
Listen for exception-logged events to trigger side effects:
Event::listen('exception-logged', function ($exception) {
// Send email, trigger webhook, etc.
});
Middleware for Global Logging: Create middleware to log exceptions globally:
public function handle($request, Closure $next)
{
try {
return $next($request);
} catch (\Exception $e) {
ExceptionLog::log($e);
throw $e;
}
}
Register it in app/Http/Kernel.php under $middleware.
Filtering Exceptions:
Use the level field to categorize exceptions (e.g., error, warning, critical) and filter logs accordingly:
ExceptionLog::log($e, ['level' => 'critical']);
How can I help you explore Laravel packages today?