异常中心异常发送器

v1.2 2021-05-08 13:58 UTC

This package is auto-updated.

Last update: 2024-09-08 21:49:42 UTC


README

将异常发送到异常中心的 Laravel 网站包。

License: MIT

安装

您可以使用 Composer 快速将此包添加到您的应用程序中。请注意,根据您的 Laravel 应用程序版本,使用正确的包版本。

版本

目前,此包支持 从 5.3 版本的所有 Laravel 版本

Composer

在 Bash 终端中

composer require exceptions-center/laravel

用法

在本节中,我们将了解如何使用当前包的功能。您需要在 app/Exceptions/Handler.php 中的 异常处理器 中添加一些行。

public function render($request, Throwable $throwable) {
    // ...

    if($this->shouldReport($throwable)) {
        ExceptionSender::make($throwable, $request)->send() ;
    }

    return parent::render($request, $throwable);
}

如果在发送过程中发生错误,错误将被保存在 storage/logs/exceptions.log 中。

配置

您有两种不同的方式来配置应用程序

方式

使用配置文件

您可以通过 config/app.php 文件访问部分可用配置

/*
|--------------------------------------------------------------------------
| Exceptions' Center environment data
|--------------------------------------------------------------------------
|
| This array is used in the Exception's Center to correctly use the
| sender. These values are not required here: you could manage your
| own sender using the publish command. You will able to define in
| the generated file the following parameters:
|
*/
'exceptions' => [
    /*
     * Key of the project.
     */
    'key' => env('EXCEPTION_CENTER_KEY'),

    /*
     * Determine whether an incoming exception will
     * be sent to the Exceptions' Center.
     */
    'enabled' => env('EXCEPTION_CENTER_ENABLED', false),

    /*
     * Determine whether the threads are allowed to
     * send the request to the Exceptions' Center.
     */
    'multithreading' => true,
    
    /*
     * Determine whether an error during the sending
     * should be saved in the storage/logs/exceptions.log 
     * file.
     */
    'log' => true,

    /*
     * If you published the ExceptionSender, you need
     * to declare the path until the generated file,
     * wherever you put it.
     */
    'model' => \ExceptionsCenter\Laravel\ExceptionSender::class,
    
    /*
     * The exception destination base URL.
     */
    'url' => env('EXCEPTION_CENTER_URL'),
],

注意:不要忘记执行 php artisan config:clear 以使更改生效。

使用已发布发送器

另一种方式是将异常发送器发布到自定义所有可能的方法。您可以使用以下命令生成 app/Exceptions/ExceptionSender.php 文件:

php artisan vendor:publish --tag=exception-sender

注意:如果您选择发布异常发送器,您需要在之前的 app 配置中定义 'model' 键,否则测试命令将不会使用您的更新。

自定义

如果您已定义配置(上一步)并发布了发送器,则会考虑配置数组。

设置

您需要定义 项目键,可在异常中心网站上找到。

多线程

当前包允许使用线程发送异常以节省执行时间。您可以在 app 配置文件中的 multithreading 键或您的已发布 ExceptionSender 中的 multithreading 属性中启用或禁用此功能。

注意:如果在启动线程时发生错误,则将使用 cURL 发送异常。

用户信息

您可以使用 getUserInformation()getGuestInformation() 方法定义有关用户的所有信息。请注意,您发送的数据。不要忘记用户的敏感信息不应离开您的应用程序,即使是用于调试。如果您正在使用 Laravel 默认 Auth 之外的其他认证守护程序,则需要重写 userIsLoggedIn() 方法。

事件监听器

如果您正在使用其他在捕获异常时触发事件的包,则可以使用 IncomingExceptionListener 来处理此事件。在您的 EventServiceProvider 中,您只需添加:

protected $listen = [
    IncomingExceptionEvent::class => [
        IncomingExceptionListener::class
    ],
];

其中 IncomingExceptionEvent 是事件名称。此事件需要实现 getException()getRequest() 方法。

测试

您可以使用 Artisan 命令检查包是否正常工作

php artisan exception:test

注意:您执行了 php artisan config:clear 吗?

架构

这是包的文件架构

.
├── composer.json
├── LICENSE
├── README.md
└── src
    ├── API
    │   ├── Contracts
    │   │   ├── ExceptionUserContract.php
    │   │   └── Reportable.php
    │   ├── ExceptionLog.php
    │   ├── Receiver
    │   │   └── ExceptionResponse.php
    │   ├── Request
    │   │   └── cUrl.php
    │   ├── Sender
    │   │   ├── AbstractExceptionSender.php
    │   │   ├── ExceptionFormatter.php
    │   │   ├── HasExceptedParameters.php
    │   │   ├── LaravelExceptionSender.php
    │   │   └── ThreadSender.php
    │   └── UrlFormatter.php
    ├── Exceptions
    │   └── CheckExceptionCenterException.php
    ├── ExceptionsCenterServiceProvider.php
    ├── ExceptionSender.php
    └── Test
        └── ExceptionTestCommand.php

8 directories, 18 files

您可以使用以下命令生成上述树:

sudo apt install tree
tree -I '.git|vendor'

许可证

本包根据 MIT 许可证 的条款。