LERN (Laravel Exception Recorder and Notifier) 是一个Laravel 5的包,它可以将异常记录到数据库中,并通过电子邮件、Pushover或Slack通知您。

安装次数: 150,284

依赖项: 4

建议者: 0

安全性: 0

星标: 441

关注者: 13

分支: 37

公开问题: 10

类型:laravel-package


README

Latest Version Software License Build Status Scrutinizer Code Quality Code Coverage Total Downloads

从错误中学习LERN

LERN是一个Laravel 5包,它可以将异常记录到数据库中并发送通知。

目前支持通过Monolog实现的以下通知通道

版本兼容性

3.x迁移到4.x

请确保配置文件现在包含新的lern.notify.classlern.record.class设置。查看配置文件了解它们的使用方法。

2.x迁移到3.x

3.x版本引入了收集更多错误信息的能力,例如user_id、url、method和输入数据。为了使用3.x,您需要复制新的配置文件、迁移文件,然后进行迁移。

# This will only copy over the migration file. For the config file you can either include the --force flag (Which will overwrite it) or copy it manually from github
php artisan vendor:publish --provider="Tylercd100\LERN\LERNServiceProvider"
php artisan migrate

安装

4.x版本使用包发现。如果您正在使用3.x,则需要遵循这些说明

使用composer安装 - 在终端

composer require tylercd100/lern

然后您需要在终端中运行以下命令以复制配置和迁移文件

php artisan vendor:publish --provider="Tylercd100\LERN\LERNServiceProvider"

在运行迁移之前,您可能想查看config/lern.php并将table属性更改为您想使用的表名。然后运行迁移

php artisan migrate

使用

要使用LERN,请修改app/Exceptions/Handler.php文件中的报告方法

public function report(Throwable $e)
{
    if ($this->shouldReport($e)) {

    	//Check to see if LERN is installed otherwise you will not get an exception.
        if (app()->bound("lern")) {
            app()->make("lern")->handle($e); //Record and Notify the Exception

            /*
            OR...
            app()->make("lern")->record($e); //Record the Exception to the database
            app()->make("lern")->notify($e); //Notify the Exception
            */
        }
    }

    return parent::report($e);
}

别忘了在文件顶部添加以下内容

//If you updated your aliases array in "config/app.php"
use LERN;
use Throwable;
//or if you didnt...
use Tylercd100\LERN\Facades\LERN;
use Throwable;

记录

您可以使用LERN::record($exception);将异常记录到数据库中。要查询任何已记录的异常,可以使用Eloquent Model的ExceptionModel

use Tylercd100\LERN\Models\ExceptionModel;
$mostRecentException = ExceptionModel::orderBy('created_at','DESC')->first();

要更改记录到数据库中的内容,请查看config/lern.php

'record'=>[
    /**
     * The Model to use
     */
    'model' => \Tylercd100\LERN\Models\ExceptionModel::class,

    /**
     * Database connection to use. Null is the default connection.
     */
    'connection'=>null,

    /**
     * Database table to use
     */
    'table'=>'vendor_tylercd100_lern_exceptions',

    /**
     * Information to store
     */
	'collect'=>[
	    'method'=>false, //When true it will collect GET, POST, DELETE, PUT, etc...
	    'data'=>false, //When true it will collect Input data
	    'status_code'=>true,
	    'user_id'=>false,
	    'url'=>false,
        'ip'=>false,
	],
],

注意:如果您更改了lern.recorder.model,则lern.recorder.tablelern.recorder.connection将被忽略,除非您扩展了\Tylercd100\LERN\Models\ExceptionModel::class

通知

LERN使用Monolog库发送通知。如果您需要比支持的通道更多的通知通道,则可以添加您自己的自定义Monolog处理器。要开始使用任何支持的处理器,只需编辑提供的配置文件config/lern.php

以编程方式更改日志级别

一些通知服务支持不同的日志级别。如果更改配置值lern.notify.log_level不够,则尝试这种方式

// Change the log level.
// Default is: critical
// Options are: debug, info, notice, warning, error, critical, alert, emergency
LERN::setLogLevel("emergency");

更改主题行

一些通知服务支持主题行,这是如何更改它的。

//Change the subject
LERN::setSubject("An Exception was thrown!");

修改通知正文

LERN 发布了一个默认的 blade 模板文件,您可以在 resources/views/exceptions/default.blade.php 找到它。该 blade 模板文件使用以下值进行编译: $exception $url $method $data $user。要指定不同的 blade 模板文件,只需编辑配置文件

'notify'=>[
    'view'=>'exceptions.default',
],
(已弃用) 使用 LERN::setMessage() 函数

请确保将视图配置值设置为 null,否则 LERN::setMessage() 将无法正常工作

'notify'=>[
    'view'=>null,
],

自定义 Monolog 处理器

要使用自定义 Monolog 处理器,请调用 pushHandler 方法

use Monolog\Handler\SlackHandler;
$handler = new SlackHandler($token, $channel);
LERN::pushHandler($handler);
LERN::notify($exception);

进一步阅读和教程

路线图

  • 支持更多 Monolog 处理器
  • 异常报告页面或命令,方便轻松识别您应用程序的问题。