cakephp-biztech / cake-sentry
CakePHP3的Sentry插件
Requires
- php: ^7.2
- cakephp/cakephp: ^4.0
- php-http/guzzle6-adapter: ^v1.1.1|^v2.0
- sentry/sentry: ^2.2
Requires (Dev)
- cakephp/cakephp-codesniffer: ^3.0
- jangregor/phpstan-prophecy: ^0.4.2
- phpstan/phpstan: @stable
- phpunit/phpunit: ^8.5.0
README
CakePHP的Sentry集成。
要求
- PHP 7.1+
- CakePHP 3.6+
- 和Sentry账户
安装
使用composer安装。
composer require cakephp-biztech/cake-sentry
使用方法
设置配置文件。
写入您的Sentry账户信息。
// in `config/app.php` return [ 'Sentry' => [ 'dsn' => YOUR_SENTRY_DSN_HERE ] ];
加载插件。
在Application.php中
public function bootstrap() { parent::bootstrap(); $this->addPlugin(\Biztech\CakeSentry\Plugin::class); }
或使用cake命令。
bin/cake plugin load Biztech/CakeSentry --bootstrap
这就完了!🎉
高级使用
忽略噪音异常
您可以过滤掉引起骚动且难以确定要解决的问题的异常(如PageNotFoundException),设置不要在Error.skipLog
中记录异常。
示例)
// in `config/app.php` 'Error' => [ 'skipLog' => [ NotFoundException::class, MissingRouteException::class, MissingControllerException::class, ], ]
参考:CakePHP菜谱 https://book.cakephp.com.cn/3.0/en/development/errors.html#error-exception-configuration
设置选项
所有配置都写入Configure::write('Sentry')
,并将传递给Sentry\init()
。请参阅Sentry的官方文档关于配置和关于php-sdk的配置。
此外,如果需要,CakeSentry还提供事件钩子,可以更容易地设置动态值。
示例)
use Cake\Event\Event; use Cake\Event\EventListenerInterface; class SentryOptionsContext implements EventListenerInterface { public function implementedEvents() { return [ 'CakeSentry.Client.afterSetup' => 'setServerContext', ]; } public function setServerContext(Event $event) { /** @var Client $subject */ $subject = $event->getSubject(); $options = $subject->getHub()->getClient()->getOptions(); $options->setEnvironment('test_app'); $options->setRelease('2.0.0@dev'); } }
在config/bootstrap.php
中
EventManager::instance()->on(new SentryOptionsContext());
发送更多上下文
在发送错误到Sentry之前,客户端会触发CakeSentry.Client.beforeCapture
事件。您可以使用EventListener和外观sentryConfigureScope()
等设置上下文,或者使用$event->getContext()->getHub()
来访问和设置上下文。调用Raven_Client的API或返回值,将发送错误上下文。现在,cake-sentry支持通过$event->getSubject()->getRequest()
在实现的事件中获取Request
实例。另请参阅官方文档中关于上下文的章节。
示例)
use Cake\Event\Event; use Cake\Event\EventListenerInterface; use Cake\Http\ServerRequest; use Cake\Http\ServerRequestFactory; use Sentry\State\Scope; use function Sentry\configureScope as sentryConfigureScope; class SentryErrorContext implements EventListenerInterface { public function implementedEvents() { return [ 'CakeSentry.Client.beforeCapture' => 'setContext', ]; } public function setContext(Event $event) { if (PHP_SAPI !== 'cli') { /** @var ServerRequest $request */ $request = $event->getData('request') ?? ServerRequestFactory::fromGlobals(); $request->trustProxy = true; sentryConfigureScope(function (Scope $scope) use ($request, $event) { $scope->setTag('app_version', $request->getHeaderLine('App-Version') ?: 1.0); $exception = $event->getData('exception'); if ($exception) { assert($exception instanceof \Exception); $scope->setTag('status', $exception->getCode()); } $scope->setUser(['ip_address' => $request->clientIp()]); $scope->setExtras([ 'foo' => 'bar', 'request attributes' => $request->getAttributes(), ]); }); } } }
在config/bootstrap.php
中
EventManager::instance()->on(new SentryErrorContext());
收集用户反馈
在CakeSentry.Client.afterCapture
事件中,您可以获取最后一个事件ID。另请参阅官方文档。
示例)
class SentryErrorContext implements EventListenerInterface { public function implementedEvents() { return [ 'CakeSentry.Client.afterCapture' => 'callbackAfterCapture', ]; } public function callbackAfterCapture(Event $event) { $lastEventId = $event->getData('lastEventId'); } }
贡献
在GitHub上欢迎pull requests和反馈 :) https://github.com/ishan-biztech/cake-sentry
许可证
该插件以开源形式提供,受MIT许可证条款的约束。