tylercd100 / lern
LERN (Laravel Exception Recorder and Notifier) 是一个Laravel 5的包,它可以将异常记录到数据库中,并通过电子邮件、Pushover或Slack通知您。
Requires
- php: ^7.2|^8.0
- illuminate/support: ^6.0|^7.0|^8.0
- monolog/monolog: ^2.0
- tylercd100/laravel-notify: ^4.0
Requires (Dev)
- doctrine/dbal: ^2.6|^3.0
- mockery/mockery: ~1.3.3|^1.4.2
- orchestra/testbench: ^4.0|^5.0|^6.0
- phpunit/phpunit: ^8.4|^9.3.3
- dev-master
- 6.0.0
- 5.0.0
- 4.5.1
- 4.5.0
- 4.4.0
- 4.3.0
- 4.2.2
- 4.2.1
- 4.2.0
- 4.1.1
- 4.1.0
- 4.0.0
- 3.8.2
- 3.8.1
- 3.8.0
- 3.7.5
- 3.7.4
- 3.7.3
- 3.7.2
- 3.7.1
- 3.7.0
- 3.6.6
- 3.6.5
- 3.6.4
- 3.6.3
- 3.6.2
- 3.6.1
- 3.6.0
- 3.5.0
- 3.4.0
- 3.3.1
- 3.3.0
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.2
- 3.0.1
- 3.0.0
- 2.3.0
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.0
- 2.0.0-beta
- 1.1.0
- 1.0.0
- dev-local
- dev-tylercd100-patch-4
- dev-tylercd100-patch-3
- dev-fix/56
- dev-fix/view-render-in-console
- dev-issue/61
- dev-set-and-get-log-level
- dev-tylercd100-patch-2
- dev-tylercd100-patch-1
- dev-3.x-to-4.x-readme-additions
- dev-fix-auto-discovery
- dev-additional-fields
- dev-5.1-support
- dev-version-4
This package is auto-updated.
Last update: 2024-09-17 14:38:24 UTC
README
从错误中学习LERN
LERN是一个Laravel 5包,它可以将异常记录到数据库中并发送通知。
目前支持通过Monolog实现的以下通知通道
- 电子邮件
- Pushover
- Slack
- Fleephook
- Flowdock
- Plivo,一个短信消息服务。
- Twilio,一个短信消息服务。
- Sentry通过Sentry SDK for PHP
- Mailgun
版本兼容性
从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 处理器
- 异常报告页面或命令,方便轻松识别您应用程序的问题。