nqxcode/laravel-exception-notifier

通过telegram和email通知laravel项目中的异常

v1.0.12 2020-11-12 11:30 UTC

This package is auto-updated.

Last update: 2024-09-16 03:48:32 UTC


README

通过 telegramemail 通知laravel项目中的异常。

通知附件包含使用 facade/ignition 渲染的 gzip压缩html 格式的异常信息,例如

Screenshot_2020-11-12 🧨 Token could not be parsed from the request

安装

在composer.json中添加此包

composer require "nqxcode/laravel-exception-notifier"

配置

通过以下命令将配置文件发布到项目中

php artisan vendor:publish --provider="Nqxcode\LaravelExceptionNotifier\ServiceProvider"

修改默认配置文件 config/laravel-exception-notifier.php,例如移除不必要的通道

<?php

return [
    'routes' => [
        [
            'channel' => 'mail',
            'route' => env('EXCEPTION_NOTIFIER_EMAIL', 'example@gmail.com'),
        ],
        [
            'channel' => 'telegram',
            'route' => env('EXCEPTION_NOTIFIER_TELEGRAM_USER_ID', '1234567890')
        ]
    ],
    'subject' => 'Исключение на сайте '.env('APP_URL'),
];

将以下内容添加到 config/services.php

<?php
return [
    
    // ...

    'telegram-bot-api' => [
        'token' => env('TELEGRAM_BOT_TOKEN'),
    ]
];

.env 中添加正确的环境变量

# For email notification
EXCEPTION_NOTIFIER_EMAIL=test@test.com

# For telegram notification
# recipient
EXCEPTION_NOTIFIER_TELEGRAM_USER_ID=423460522
# sender
TELEGRAM_BOT_TOKEN=1160101879:AAFzuda0o7X6Dp4RBp00K-7dYjjnwMY887A

要获取 telegram机器人令牌,在 BotFather 中创建新的机器人,使用此操作 /newbot,更多详情请见 这里

要在 production 环境中通知异常,修改文件 app/Exceptions/Handler.php 中的 report 方法

<?php
namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Nqxcode\LaravelExceptionNotifier\ExceptionNotifierInterface;
use Throwable;

class Handler extends ExceptionHandler
{
    // ...

    public function report(Throwable $exception)
    {
        if ($this->container->isProduction() && $this->shouldReport($exception)) {
            $this->container->make(ExceptionNotifierInterface::class)->notify($exception);
        }

        parent::report($exception);
    }
    
    // ...
}

许可证

该软件包许可在MIT许可证下。