zanysoft/laravel-exception-monitor

一个小型Laravel包,用于通知您应用中发生的异常。

1.0.1 2023-07-26 11:04 UTC

This package is auto-updated.

Last update: 2024-09-04 14:44:30 UTC


README

Downloads License GitHub tag

Laravel异常监控包

该包会在您的生产应用中抛出异常时通知您。

Slack Preview

安装

composer require zanysoft/laravel-exception-monitor

如果您使用的是Laravel 5.4或更早版本,需要将以下内容添加到您的 config/app.php 文件中(对于Laravel 5.5及以上版本,这些配置将自动由Laravel发现)

'providers' => [
    //...
    ZanySoft\LaravelExceptionMonitor\ExceptionMonitorServiceProvider::class,
],

'aliases' => [
    //...
    'ExceptionMonitor' => ZanySoft\LaravelExceptionMonitor\Facades\ExceptionMonitor::class,
];

发布包的配置和视图文件到您的应用中。请在终端运行以下命令。

php artisan vendor:publish --provider="ZanySoft\LaravelExceptionMonitor\ExceptionMonitorServiceProvider"

您需要设置入站Webhooks以发送消息到Slack。

配置

配置文件非常直观。

<?php

return [
    /*
     |--------------------------------------------------------------------------
     | Enabled sender drivers
     |--------------------------------------------------------------------------
     |
     | Send a notification about exception in your application to supported channels.
     |
     | Supported: "mail", "slack". You can use multiple drivers.
     |
     */
    'drivers'      => [ 'mail', 'slack' ],

    /*
     |--------------------------------------------------------------------------
     | Enabled application environments
     |--------------------------------------------------------------------------
     |
     | Set environments that should generate notifications.
     |
     */
    'environments' => [ 'production'],

    /*
     |--------------------------------------------------------------------------
     | Disable Error Notifications
     |--------------------------------------------------------------------------
     |
     | Set status code for disable notifications. like [401,404,500] 
     |
     */
    'skip_error' => [401, 404, 405, 500],

    /*
     |--------------------------------------------------------------------------
     | Mail Configuration
     |--------------------------------------------------------------------------
     |
     | It uses your app default Mail driver. You shouldn't probably touch the view
     | property unless you know what you're doing.
     |
     */
    'mail'         => [
        'from' => 'sender@example.com',
        'to'   => 'recipient@example.com',
        'view' => 'mails/exception-monitor'
    ],

    /*
     * set endpoint url from Incoming WebHooks https://my.slack.com/services/new/incoming-webhook
     */
    'slack' => [
        'endpoint' => 'https://hooks.slack.com/services/....',
        'channel' => '#bugtracker',
        'username' => 'Exception Monitor',
        'icon' => ':robot_face:',
    ],
];

用法

要开始捕获异常,您有两种选择。

第一种选项:扩展包提供的异常处理程序(app/Exceptions/Handler.php

use ZanySoft\LaravelExceptionMonitor\MonitorExceptionHandler;
...
class Handler extends MonitorExceptionHandler

第二种选项:使您的report方法在app/Exceptions/Handler.php中看起来像这样

public function report(Exception $e)
{
    foreach ($this->dontReport as $type) {
        if ($e instanceof $type) {
            return parent::report($e);
        }
    }

    if (app()->bound('exception-monitor')) {
        app('exception-monitor')->notifyException($e);
    }
  
    // OR
  
    ExceptionMonitor::notifyException($e);

    parent::report($e);
}

许可证

本库采用MIT许可证。请参阅许可证文件以获取更多信息。