Installation
composer require bazoonchik/critical-alerts
Publish the config file:
php artisan vendor:publish --provider="Bazoonchik\CriticalAlerts\CriticalAlertsServiceProvider" --tag="config"
Configuration
Edit config/critical-alerts.php to define your Telegram and Sentry credentials:
'telegram' => [
'bot_token' => env('TELEGRAM_BOT_TOKEN'),
'chat_id' => env('TELEGRAM_CHAT_ID'),
],
'sentry' => [
'dsn' => env('SENTRY_DSN'),
'level' => env('SENTRY_LOG_LEVEL', 'error'),
],
First Use Case: Logging to Telegram
Inject the CriticalAlerts facade into your controller/service:
use Bazoonchik\CriticalAlerts\Facades\CriticalAlerts;
public function handleCriticalError()
{
CriticalAlerts::telegram('Application crashed!')->log();
}
Channel-Specific Logging
Use predefined channels (telegram, sentry) for structured alerts:
CriticalAlerts::telegram('Database connection failed')->log();
CriticalAlerts::sentry(new \Exception('API timeout'))->log();
Dynamic Context Attachment Pass metadata (e.g., user ID, request data) for richer debugging:
CriticalAlerts::telegram('Payment failed')
->withContext(['user_id' => 123, 'amount' => 99.99])
->log();
Integration with Monolog Extend Laravel’s default logging to auto-forward critical logs:
// In app/Providers/AppServiceProvider.php
public function boot()
{
$this->app['log']->extend('critical', function () {
return new \Bazoonchik\CriticalAlerts\Monolog\CriticalHandler();
});
}
Then use in code:
\Log::critical('Critical failure', ['context' => 'key']);
Bazoonchik\CriticalAlerts\Contracts\AlertChannel for custom integrations (e.g., Slack, Email).config/critical-alerts.php to restrict alerts by severity:
'levels' => ['error', 'critical'], // Only log these levels
Environment Variables
TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID, and SENTRY_DSN are set in .env. Missing values will silently fail.AppServiceProvider@boot():
if (!config('critical-alerts.telegram.bot_token')) {
throw new \RuntimeException('Telegram token not configured!');
}
Sentry Rate Limits
->withLevel('critical') sparingly for high-severity issues only.Telegram Message Length
->withContext() for details and log the truncated message separately.config/critical-alerts.php:
'enabled' => [
'telegram' => true,
'sentry' => true,
],
http://localhost:3000 for debugging.Custom Formatting
Override the default message formatter by binding a new Bazoonchik\CriticalAlerts\Contracts\MessageFormatter:
// In AppServiceProvider@register()
$this->app->bind(\Bazoonchik\CriticalAlerts\Contracts\MessageFormatter::class, function () {
return new \App\Services\CustomFormatter();
});
Queue Delay for Non-Urgent Alerts Use Laravel Queues to defer non-critical alerts:
CriticalAlerts::telegram('Scheduled job failed')->delay(now()->addMinutes(5))->log();
How can I help you explore Laravel packages today?