Setting Up Alerts for Laravel Exceptions
Never miss a production error again. Learn how to configure alerts that notify you when Laravel exceptions occur.
Waiting for users to report errors is a losing strategy. By the time they complain, you've already lost their trust. Setting up automated alerts ensures you know about problems the moment they occur.
Alert Strategy
Not all errors deserve the same attention. Structure your alerts:
| Severity | Examples | Response |
|---|---|---|
| Critical | Database down, payment failures | Immediate (phone/SMS) |
| Error | Unhandled exceptions | Within hours (email/Slack) |
| Warning | Slow queries, deprecations | Next business day |
Option 1: Using a Log Management Service
Services like 401 Clicks include built-in alerting:
- Navigate to Alerts in your project
- Create a new alert rule
- Set conditions (e.g., level = error)
- Choose notification channels (email, Slack, Discord)
- Set a threshold (e.g., more than 5 errors in 5 minutes)
Option 2: Laravel's Exception Handler
<?php
// app/Exceptions/Handler.php
public function register(): void
{
$this->reportable(function (Throwable $e) {
if ($this->shouldAlert($e)) {
$this->sendAlert($e);
}
});
}
protected function shouldAlert(Throwable $e): bool
{
return !$this->shouldntReport($e)
&& app()->environment('production');
}
protected function sendAlert(Throwable $e): void
{
Notification::route('slack', config('services.slack.webhook'))
->notify(new ExceptionOccurred($e));
}
Slack Notification Class
<?php
namespace App\Notifications;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\SlackMessage;
class ExceptionOccurred extends Notification
{
public function __construct(public \Throwable $exception) {}
public function via($notifiable): array
{
return ['slack'];
}
public function toSlack($notifiable): SlackMessage
{
return (new SlackMessage)
->error()
->content('Exception in ' . config('app.name'))
->attachment(function ($attachment) {
$attachment
->title(get_class($this->exception))
->content($this->exception->getMessage())
->fields([
'File' => $this->exception->getFile() . ':' . $this->exception->getLine(),
'URL' => request()->fullUrl(),
]);
});
}
}
Rate Limiting Alerts
Avoid alert fatigue by rate limiting:
use Illuminate\Support\Facades\Cache;
protected function sendAlert(Throwable $e): void
{
$key = 'alert:' . md5(get_class($e) . $e->getMessage());
if (Cache::has($key)) {
return; // Already alerted recently
}
Cache::put($key, true, now()->addMinutes(15));
// Send the alert
}
Best Practices
- Don't alert on everything: Only critical and error levels
- Include context: URL, user, stack trace
- Rate limit: Prevent notification floods
- Test your alerts: Trigger a test error to verify
- Document response: What should happen when alerts fire?
Conclusion
Automated alerts transform how you handle production issues. Instead of reactive firefighting, you're proactively fixing problems—often before users notice them.
Admin
Published on January 13, 2026