中国档案网站建设现状研究,陕西建设厅执业资格注册中心网站,网站制作的内容包含,seo的范畴是什么tp8的默认使用的就是composer来管理第三方包, 所以直接使用 composer 来安装 sentry9 即可. 同时tp8和tp5的配置方式不太一样, 这里我们直接使用自定义异常类来处理Sentry的异常.
1. 安装 sentry9 包
# 安装 sentry9 包
composer require tekintian/sentry9-php …tp8的默认使用的就是composer来管理第三方包, 所以直接使用 composer 来安装 sentry9 即可. 同时tp8和tp5的配置方式不太一样, 这里我们直接使用自定义异常类来处理Sentry的异常.
1. 安装 sentry9 包
# 安装 sentry9 包
composer require tekintian/sentry9-php
2. 创建tp8的自定义异常处理类
位置 app\common\exception 目录下创建 Sentry.php 文件
Sentry.php自定义异常类的内容如下
?php
namespace app\common\exception;use think\exception\Handle;
use think\exception\HttpException;
use think\exception\ValidateException;
use think\Response;
use Throwable;
use Sentry9\Client;class Sentry extends Handle
{/*** var Client*/private $sentry;/*** 获取Sentry异常处理实例** return Client*/public function getSentry(): Client {if (is_null($this-sentry)) {// 初始化Sentry$dsn config(app.sentry_dsn);$this-sentry \Sentry9\Sentry::listen($dsn);}return $this-sentry;}/*** 记录异常信息包括日志或者其它方式记录** access public* param Throwable $exception* return void*/public function render($request, Throwable $e): Response{// 获取Sentry异常处理实例$sentry $this-getSentry();// 记录异常信息 放在这里就是记录所有的异常信息, 如果放在 if里面的话就可以只记录指定类型的异常信息$sentry-captureException($e);// 参数验证错误if ($e instanceof ValidateException) {// 记录错误日志return json($e-getError(), 422);}// 请求异常if ($e instanceof HttpException $request-isAjax()) {return response($e-getMessage(), $e-getStatusCode());}// 其他错误交给系统处理return parent::render($request, $e);}}
3. 在app/provider.php中加载自定义异常类
就是将 think\exception\Handle \app\common\exception\Sentry, 添加到app/provider.php中 这里就会自动覆盖tp8的异常处理类. 4. 修改tp8配置文件 config/app.php,增加SENTRY配置项目 在config/app.php文件的最后增加sentry9配置项目 // sentry DSN配置sentry_dsn env(SENTRY_DSN, https://xxxxxxxsentry.tekin.cn/2), 5. 在项目根目录的.env文件中增加SENTRY_DSN 配置
SENTRY_DSN https://xxxxxxxsentry.tekin.cn/2到此我们就完成了thinkphp8中的sentry9的配置, 是不是很简单.