webmaster-hm / lern81
LERN(Laravel异常记录器和通知器)是一个Laravel 5包,它将异常记录到数据库中,并通过电子邮件、Pushover或Slack通知您。
Requires
- php: ^7.2|^8.0|^8.1
- laravel/framework: ^10.0
- monolog/monolog: ^3.0
Requires (Dev)
- doctrine/dbal: ^3.0
- mockery/mockery: ^1.6.7
- phpunit/phpunit: ^10.0
README
从错误中学习LERN
LERN是一个Laravel 5包,它将异常记录到数据库中,并会发送通知。
目前通过Monolog支持以下通知渠道
版本兼容性
从3.x
迁移到4.x
请确保配置文件现在包括新的lern.notify.class
和lern.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.table
和lern.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 处理器
- 异常报告页面或命令,方便您轻松识别应用程序的问题。