kevincobain2000 / laravel-alert-notifications
Alert notifications of exceptions from your laravel application
Installs: 16 262
Dependents: 0
Suggesters: 0
Security: 0
Stars: 26
Watchers: 4
Forks: 10
Open Issues: 0
Requires
- php: ^7.3|^8.0
- guzzlehttp/guzzle: ^6.3|^7.0
- illuminate/support: ^7.0|^8.79|^9.33|^10.0|^11.0
Requires (Dev)
- mockery/mockery: ^1.3.2
- orchestra/testbench: ^5.0|^6.0|^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.0|^10.0
- squizlabs/php_codesniffer: ^3.0
README
Laravel Alert Notifcations
Send php exceptions to email, microsoft teams, slack.
Notifications are throttle enabled so devs don't get a lot of emails from one host
(or all hosts if cache driver is shared).
Installation
composer require kevincobain2000/laravel-alert-notifications
If you're using Laravel 5.5+ let the package auto discovery make this for you.
'providers' => [
\Kevincobain2000\LaravelAlertNotifications\AlertNotificationsServiceProvider::class
]
Tests
composer install
composer run test
Publish (Laravel)
php artisan vendor:publish --provider="Kevincobain2000\LaravelAlertNotifications\AlertNotificationsServiceProvider"
php artisan config:cache
Publish (Lumen)
Since Lumen doesn't support auto-discovery, move config and view files to the destination directories manually
cp vendor/kevincobain2000/laravel-alert-notifications/src/config/laravel_alert_notifications.php config/laravel_alert_notifications.php mkdir -p "resources/views/vendor/laravel_alert_notifications/" && cp vendor/kevincobain2000/laravel-alert-notifications/src/views/* resources/views/vendor/laravel_alert_notifications/
and add the following to bootstrap/app.php:
$app->register(Kevincobain2000\LaravelAlertNotifications\AlertNotificationsServiceProvider::class);
.env
ALERT_NOTIFICATION_MAIL_FROM_ADDRESS=
ALERT_NOTIFICATION_MAIL_TO_ADDRESS=
ALERT_NOTIFICATION_MAIL_NOTICE_TO_ADDRESS=
ALERT_NOTIFICATION_MAIL_WARNING_TO_ADDRESS=
ALERT_NOTIFICATION_MAIL_ERROR_TO_ADDRESS=
ALERT_NOTIFICATION_MAIL_CRITICAL_TO_ADDRESS=
ALERT_NOTIFICATION_MAIL_ALERT_TO_ADDRESS=
ALERT_NOTIFICATION_MAIL_EMERGENCY_TO_ADDRESS=
ALERT_NOTIFICATION_CACHE_DRIVER=file
ALERT_NOTIFICATION_MICROSOFT_TEAMS_WEBHOOK=
ALERT_NOTIFICATION_SLACK_WEBHOOK=
ALERT_NOTIFICATION_CURL_PROXY=
Usage
new AlertDispatcher( Exception $e [, array $dontAlertExceptions = []] // Exceptions that shouldn't trigger notifications [, array $notificationLevelsMapping = []] // [Exception class => Notification level] mapping [, array $exceptionContext = []] // Array of context data )
In app/Exceptions/Handler.php. It is better to use a try catch to prevent loop.
use Kevincobain2000\LaravelAlertNotifications\Dispatcher\AlertDispatcher; class Handler extends ExceptionHandler { private $exceptionLogLevels = [ DebugLevelException::class => LogLevel::DEBUG, WarningLevelException::class => LogLevel::WARNING, ErrorLevelException::class => LogLevel::ERROR, ]; protected $dontReport = [ // ]; public function report(Throwable $exception) { try { $dontReport = array_merge($this->dontReport, $this->internalDontReport); $alertDispatcher = new AlertDispatcher($exception, $dontReport, $this->exceptionLogLevels); $alertDispatcher->notify(); } catch (Throwable $e) { // log any unexpected exceptions or do nothing } parent::report($exception); } }
Config
Samples
Teams
Slack
References
- https://qiita.com/kidatti/items/8732114ec4d1727844b8
- https://laravel-news.com/email-on-error-exceptions
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!