kevincobain2000/laravel-alert-notifications

从您的 Laravel 应用程序中捕获异常的通知

6.0 2024-07-21 10:56 UTC

This package is auto-updated.

Last update: 2024-09-21 11:34:27 UTC


README

Laravel Alert Notifications

将 PHP 异常发送到电子邮件、Microsoft Teams 和 Slack。
通知具有节流功能,因此开发者不会收到来自一个主机(或所有主机,如果缓存驱动器是共享的)的大量电子邮件。
(或所有主机,如果缓存驱动器是共享的)。

All Contributors

composer-install-time coverage composer-dependencies

composer-install-time coverage composer-dependencies

安装

composer require kevincobain2000/laravel-alert-notifications

如果您使用的是 Laravel 5.5+,让包的自动发现为您完成这项工作。

'providers' => [
    \Kevincobain2000\LaravelAlertNotifications\AlertNotificationsServiceProvider::class
]

测试

composer install
composer run test

发布(Laravel)

php artisan vendor:publish --provider="Kevincobain2000\LaravelAlertNotifications\AlertNotificationsServiceProvider"
php artisan config:cache

发布(Lumen)

由于 Lumen 不支持自动发现,请手动将配置和视图文件移动到目标目录,并将以下内容添加到 bootstrap/app.php 中

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/

$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=

用法

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
)

app/Exceptions/Handler.php 中。最好使用 try-catch 来防止循环。

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);
    }
}

配置

示例

电子邮件

Email

团队

Teams

Slack

Slack

参考

  1. https://qiita.com/kidatti/items/8732114ec4d1727844b8
  2. https://news.laravel.net.cn/email-on-error-exceptions

贡献者 ✨

感谢这些出色的人(emoji key

本项目遵循 all-contributors 规范。欢迎任何形式的贡献!