bzilee/multichannel-log-notification
Laravel package to send log notifications over multiple channels (Telegram, email, SMS, HTTP). Configure per log level, enable via env, plug into Monolog as a custom channel, and dispatch notifications to a dedicated queue for performance.
Installation
composer require bzilee/multichannel-log-notification
Publish the config file:
php artisan vendor:publish --provider="Bzilee\MultichannelLogNotification\MultichannelLogNotificationServiceProvider" --tag="config"
Configure Channels
Edit config/multichannel-log-notification.php to define your channels (e.g., Telegram, HTTP, SMS, Email) with their respective credentials and endpoints.
First Use Case Log a notification via a channel:
use Bzilee\MultichannelLogNotification\Facades\MultichannelLogNotification;
MultichannelLogNotification::send('telegram', 'Error in API', ['user_id' => 123]);
config/multichannel-log-notification.php – Channel configurations.app/Providers/AppServiceProvider.php – Register any custom channels (if needed).routes/web.php – If using HTTP channel, ensure the endpoint is exposed.Channel Registration
Extend the package by registering custom channels in AppServiceProvider:
public function boot()
{
MultichannelLogNotification::extend('slack', function ($app) {
return new \Bzilee\MultichannelLogNotification\Channels\SlackChannel();
});
}
Sending Notifications
MultichannelLogNotification::send('telegram', 'Deployment failed', ['commit' => 'abc123']);
MultichannelLogNotification::sendToChannels(['telegram', 'email'], 'Critical error', ['trace' => $exception->getTraceAsString()]);
Logging Context Attach metadata (e.g., user IDs, timestamps) for debugging:
MultichannelLogNotification::send('http', 'Payment processed', [
'user_id' => auth()->id(),
'amount' => $order->amount,
'timestamp' => now()->toIso8601String(),
]);
Integration with Laravel Logging Hook into Laravel’s log system to auto-send notifications:
use Illuminate\Support\Facades\Log;
Log::channel('multichannel')->error('Database connection failed', ['context' => 'production']);
Configure the multichannel log channel in config/logging.php:
'channels' => [
'multichannel' => [
'driver' => 'custom',
'via' => \Bzilee\MultichannelLogNotification\Logging\MultichannelHandler::class,
],
],
throttle:60,1 for HTTP channel)..env variables for sensitive data (e.g., TELEGRAM_BOT_TOKEN).Channel Misconfiguration
'debug' => true) and check Laravel logs (storage/logs/laravel.log).if (!MultichannelLogNotification::isChannelConfigured('telegram')) {
throw new \RuntimeException('Telegram channel not configured!');
}
Payload Formatting
MultichannelLogNotification::send('telegram', 'Alert', ['message' => '```' . $error . '```']);
HTTP Channel CORS
VerifyCsrfToken middleware if needed.SMS Gateway Delays
MultichannelLogNotification::send('sms', 'OTP: 12345', [], ['retries' => 3]);
log_sent config option to track all dispatched notifications:
'telegram' => [
'token' => env('TELEGRAM_BOT_TOKEN'),
'log_sent' => true, // Logs to storage/logs/multichannel.log
],
null channel for unit tests:
MultichannelLogNotification::extend('null', function () {
return new class {
public function send($message, $context) {}
};
});
Custom Channel Development
Extend the base Channel class:
namespace App\Channels;
use Bzilee\MultichannelLogNotification\Contracts\Channel;
class CustomChannel implements Channel {
public function send($message, $context) {
// Implement logic (e.g., Webhook to Datadog)
}
}
Register it in AppServiceProvider.
Middleware for Pre/Post-Processing Add middleware to modify messages or contexts:
MultichannelLogNotification::extend('telegram', function () {
return tap(new \Bzilee\MultichannelLogNotification\Channels\TelegramChannel(), function ($channel) {
$channel->pushMiddleware(new class {
public function handle($message, $context, callable $next) {
$context['processed_at'] = now();
return $next($message, $context);
}
});
});
});
Dynamic Channel Selection Use environment variables or user input to switch channels:
$channel = config('multichannel-log-notification.default') ?? 'telegram';
MultichannelLogNotification::send($channel, 'Dynamic alert');
How can I help you explore Laravel packages today?