tylercd100/monolog-sms
Laravel/Lumen package that adds an SMS handler to Monolog, letting you send log alerts and critical errors via text message using popular SMS gateways. Useful for on-call notifications when something breaks in production.
Installation
composer require tylercd100/monolog-sms
Basic Configuration
Add the SMS handler to your Monolog configuration in config/logging.php:
'channels' => [
'sms' => [
'driver' => 'sms',
'provider' => env('SMS_PROVIDER', 'twilio'),
'from' => env('SMS_FROM', '+1234567890'),
'to' => env('SMS_TO', '+1987654321'),
'options' => [
'account_sid' => env('TWILIO_SID'),
'auth_token' => env('TWILIO_TOKEN'),
],
],
],
First Use Case Log an SMS alert via the logger facade:
Log::channel('sms')->alert('Database connection failed!');
Conditional SMS Alerts Use Monolog’s levels to control when SMS alerts fire:
if ($criticalError) {
Log::channel('sms')->emergency('Critical failure detected!');
}
Dynamic Recipients
Override the to field dynamically in runtime:
$handler = new \TylerCD100\MonologSms\Handler\SmsHandler(
new \Monolog\Logger('sms'),
'+1' . $user->phone_number,
$provider
);
$logger->pushHandler($handler);
Structured Logging Attach context to SMS messages for debugging:
Log::channel('sms')->error('Payment failed', [
'user_id' => $user->id,
'amount' => $amount,
'transaction_id' => $txnId,
]);
Queue SMS for Async Delivery Pair with Laravel Queues to avoid blocking requests:
Log::channel('sms')->debug('Queued SMS alert'); // Log to file first
dispatch(new SendSmsJob($message, $recipient));
Rate Limiting
Use Monolog’s Filter to throttle SMS alerts:
$filter = new \Monolog\Filter\CallbackFilter(function ($record) {
return !Cache::has('sms_alert_throttle');
});
$handler->pushFilter($filter);
Fallback Channels Combine with other channels (e.g., Slack) for redundancy:
Log::channels(['sms', 'slack'])->error('High severity alert!');
Provider-Specific Quirks
from number to be verified. Unverified numbers may fail silently.Message Formatting
$handler->setFormatter(new \Monolog\Formatter\LineFormatter(
"%message%\n[Context: %context%]",
['date' => 'Y-m-d H:i:s']
));
Error Handling
try {
Log::channel('sms')->alert('Error!');
} catch (\Exception $e) {
Log::channel('single')->error('SMS failed: ' . $e->getMessage());
}
Environment-Specific Config
to numbers. Use feature flags or environment variables:
'to' => env('SMS_TO', config('services.sms.default_recipient')),
Log Provider Responses Enable debug mode for the SMS provider (e.g., Twilio’s debug logs) to inspect API responses.
$provider->setDebug(true); // If supported by your provider wrapper.
Test with a Fake Handler
Replace the SMS handler with a StreamHandler during development:
$handler = new \Monolog\Handler\StreamHandler(storage_path('logs/sms_test.log'));
$logger->pushHandler($handler);
Check for Silent Failures Monitor your SMS provider’s dashboard for undelivered messages or errors.
Custom Providers Extend the base handler to support new providers:
class CustomSmsHandler extends \TylerCD100\MonologSms\Handler\SmsHandler {
protected function sendSms($message, $to) {
// Implement custom logic (e.g., HTTP client calls)
}
}
Message Templates Use Laravel’s Blade or a templating library to pre-process messages:
$message = view('logs.sms_template', ['error' => $record->message])->render();
Webhook Fallback Combine with a webhook handler to retry failed SMS via email or push notifications:
Log::channel('webhook')->pushHandler(
new \Monolog\Handler\PushHandler([
new \Monolog\Handler\SmsHandler(...),
new \Monolog\Handler\HttpHandler(env('WEBHOOK_URL')),
])
);
How can I help you explore Laravel packages today?