Installation:
composer require analogic/alert-bundle
Add to config/bundles.php (Laravel 5.5+):
return [
// ...
Analogic\AlertBundle\AnalogicAlertBundle::class => ['all' => true],
];
Basic Configuration (config/alert.php):
return [
'enabled' => env('APP_ENV') !== 'local',
'prefix' => '[ALERT] ',
'from' => [
'email' => 'alerts@yourdomain.com',
'name' => 'App Alerts',
],
'to' => ['admin@example.com'],
'ignore' => [
\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class,
\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException::class,
],
];
First Use Case:
enabled: true).<script>{{ javascript_error_listener() }}</script>
Exception Handling:
try-catch or leverage middleware (e.g., App\Middleware\AlertHandler):
public function handle($request, Closure $next) {
try {
return $next($request);
} catch (\Exception $e) {
if (!in_array(get_class($e), config('alert.ignore'))) {
alert($e); // Built-in helper (if provided)
}
throw $e;
}
}
-e prod to bypass dev environment checks:
php artisan your:command -e prod
JS Error Tracking:
resources/views/layouts/app.blade.php).window.addEventListener('error', (event) => {
fetch('/api/alerts/js', {
method: 'POST',
body: JSON.stringify({
error: event.message,
url: window.location.href,
user: {{ json_encode(session('user')) }},
}),
});
});
Email Spooling:
config/services.php:
'mailer' => [
'dsn' => 'file:///tmp/alerts',
],
php bin/console messenger:consume async -vv (or use incron for real-time).AppKernel with Laravel’s service provider (AnalogicAlertServiceProvider).Mail facade for custom email templates:
use Illuminate\Support\Facades\Mail;
Mail::raw($alert->getMessage(), function ($message) {
$message->to(config('alert.to'))
->subject(config('alert.prefix') . 'Alert');
});
Log::critical() for redundancy:
if (!alert($e)) {
Log::critical('Alert failed', ['exception' => $e]);
}
Environment Mismatch:
config/alert.php but still triggered in production.env('APP_ENV') !== 'local' and verify APP_ENV in .env.AlertListener to confirm environment checks.Ignored Exceptions:
DatabaseException) are silently ignored.ignore array or use a wildcard:
'ignore' => [
'*HttpException', // Ignores all HTTP exceptions
'!\\Symfony\\Component\\HttpKernel\\Exception\\HttpExceptionInterface', // Exclude specific types
],
JS Listener Conflicts:
window.addEventListener('error', (e) => {
if (e.message.includes('AnalogicAlert')) return;
// Your logic
});
Spool Overload:
* * * * * php bin/console messenger:consume async --limit=100
Check Listener Registration:
AlertListener is subscribed to kernel events in AnalogicAlertBundle::boot().handleException():
\Log::debug('Alert triggered', ['exception' => $exception]);
Email Delivery:
'mailer' => [
'dsn' => 'smtp://mailhog:1025',
],
Custom Alert Channels:
AlertManager:
class CustomAlertManager extends \Analogic\AlertBundle\Manager\AlertManager {
public function send(\Exception $exception) {
// Custom logic (e.g., Slack API call)
parent::send($exception); // Fallback to email
}
}
AnalogicAlertServiceProvider.Dynamic Recipients:
config('alert.to') to fetch recipients from a database:
'to' => function () {
return \App\Models\User::where('role', 'admin')->pluck('email')->toArray();
},
Rich Error Context:
$exception = new \RuntimeException('Oops!');
$exception->setContext(['user_id' => auth()->id(), 'request' => $request->all()]);
alert($exception);
AlertFormatter.How can I help you explore Laravel packages today?