Installation
Run composer require deslynx/alert-bundle in your Laravel project.
No additional configuration is required for basic usage (Symfony Flex auto-registers the bundle).
First Use Case: Logging Alerts
Inject the AlertService into a controller or service:
use DesLynx\AlertBundle\Service\AlertService;
public function __construct(private AlertService $alertService) {}
public function handleCriticalError()
{
$this->alertService->alert('Database connection failed', 'error');
}
This sends an email alert via the configured Monolog handler.
Where to Look First
config/alert.php (auto-generated by Flex).AlertService (main entry point).DesLynx\AlertBundle\Handler\AlertHandler (for custom logging).Sending Alerts Programmatically
Use AlertService to trigger alerts with context:
$this->alertService->alert(
'Payment gateway timeout',
'warning',
['user_id' => 123, 'amount' => 99.99]
);
error, warning, info (maps to Monolog levels).Integrating with Monolog
The bundle adds an AlertHandler to your Monolog stack. Customize it in config/logging.php:
'channels' => [
'alert' => [
'type' => 'custom',
'path' => DesLynx\AlertBundle\Handler\AlertHandler::class,
],
],
Log alerts directly via the Log facade:
\Log::alert('Critical failure', ['context' => 'data']);
Email Templates
Override default templates in resources/views/vendor/alert/:
alert_email.html.twig (Symfony Twig) or alert_email.blade.php (Laravel Blade).$alert (e.g., {{ $alert.level }}, {{ $alert.message }}).Queueing Alerts
Disable immediate email sending by setting 'sync' => false in config/alert.php and queue the SendAlertEmail job:
$this->alertService->queueAlert('Scheduled maintenance', 'info');
AlertService to fetch recipients from a database:
$recipients = User::where('role', 'admin')->pluck('email');
$this->alertService->setRecipients($recipients);
AlertTransport (see Extension Points).Configuration Overrides
config/alert.php:
'from' => 'alerts@example.com', // Required
'to' => ['admin@example.com'], // Default recipients
'sync' => true, // Set to false for queued alerts
config/logging.php includes the alert channel.Monolog Handler Conflicts
config/logging.php. The AlertHandler must be added to a channel (e.g., single or stack).StreamHandler to the alert channel to verify logs:
'alert' => [
'type' => 'stream',
'path' => storage_path('logs/alert.log'),
'level' => 'debug',
],
Template Caching
php artisan view:clear
alert_email.blade.php path is correct (Laravel may not auto-discover Symfony’s vendor/ views).Queue Failures
sync: false), ensure the SendAlertEmail job is registered in App\Console\Kernel:
protected $commands = [
\DesLynx\AlertBundle\Command\SendAlertEmail::class,
];
Log::alert(), Log::warning(), etc., to ensure the correct Monolog level is triggered.null.Custom Transports
Implement DesLynx\AlertBundle\Transport\AlertTransportInterface to add Slack, PagerDuty, etc.:
class SlackTransport implements AlertTransportInterface {
public function send(Alert $alert): void {
// Send to Slack webhook
}
}
Register it in config/alert.php:
'transports' => [
'email' => DesLynx\AlertBundle\Transport\EmailTransport::class,
'slack' => App\Transport\SlackTransport::class,
],
Alert Levels
Extend the AlertLevel enum (Symfony) or override levels in config/alert.php:
'levels' => [
'critical' => \Monolog\Logger::ERROR,
'high' => \Monolog\Logger::WARNING,
],
Monolog Processor Add a custom processor to modify alert data before sending:
// config/logging.php
'processors' => [
DesLynx\AlertBundle\Processor\AlertProcessor::class,
],
How can I help you explore Laravel packages today?