mindaugasjackunaspc / sentry-module
此模块允许将Sentry客户端集成到laminas和mezzio中
3.0.1
2021-08-25 21:18 UTC
Requires
- php: ^7.2
- ext-json: *
- laminas/laminas-servicemanager: ^3.4.0
- sentry/sentry: ^3.0.0
Requires (Dev)
- facile-it/facile-coding-standard: ^0.3.1 || 0.4.0
- friendsofphp/php-cs-fixer: ^2.16
- laminas/laminas-eventmanager: ^3.2.1
- laminas/laminas-log: ^2.12.0
- laminas/laminas-modulemanager: ^2.8.4
- laminas/laminas-mvc: ^3.1.1
- laminas/laminas-view: ^2.11.4
- php-http/curl-client: ^2.1.0
- phpspec/prophecy: ^1.10
- phpunit/phpunit: ^7.5 || ^8.0
- vimeo/psalm: ^4.1
Suggests
- laminas/laminas-eventmanager: Install to use the MVC error handler
- laminas/laminas-log: Install to use the log writer
Conflicts
- container-interop/container-interop: < 1.2
- laminas/laminas-eventmanager: < 2.6.2
- laminas/laminas-log: < 2.9
- laminas/laminas-modulemanager: < 2.5.1
- laminas/laminas-mvc: < 2.7
- laminas/laminas-view: < 2.5.1
README
此模块允许将Sentry客户端集成到laminas和mezzio中。
安装
安装此模块的唯一支持方式是通过composer。有关composer文档,您可以参考getcomposer.org。
使用php-http/async-client-implementation
和psr/http-message-implementation
实现安装
php composer.phar require facile-it/sentry-module php-http/curl-client laminas/laminas-diactoros
配置
客户端
要配置客户端实例,您可以按以下方式操作
return [ 'sentry' => [ 'disable_module' => false, 'options' => [ 'dsn' => '', // other sentry options // https://docs.sentry.io/error-reporting/configuration/?platform=php ], 'javascript' => [ 'inject_script' => false, 'script' => [ 'src' => 'https://#/5.6.3/bundle.min.js', 'integrity' => 'sha384-/Cqa/8kaWn7emdqIBLk3AkFMAHBk0LObErtMhO+hr52CntkaurEnihPmqYj3uJho', 'crossorigin' => 'anonymous', ], 'options' => [ 'dsn' => '', // other sentry options // https://docs.sentry.io/error-reporting/configuration/?platform=php ], ], ], ]; //...
现在您可以通过从服务定位器中检索它来使用客户端和Hub。
use Sentry\HubInterface; $hub = $this->getServiceLocator()->get(HubInterface::class);
错误处理器监听器
此模块提供了对MvcEvent::EVENT_DISPATCH_ERROR
和MvcEvent::EVENT_RENDER_ERROR
事件的监听,以便记录这些事件中捕获的异常。
如果您想使用它,应在您的应用程序模块中注册它。
示例
<?php namespace App; use Facile\SentryModule\Listener\ErrorHandlerListener; use Laminas\Mvc\MvcEvent; class Module { public function onBootstrap(MvcEvent $e): void { $application = $e->getApplication(); $container = $application->getServiceManager(); $eventManager = $application->getEventManager(); /** @var ErrorHandlerListener $errorHandlerListener */ $errorHandlerListener = $container->get(ErrorHandlerListener::class); $errorHandlerListener->attach($eventManager); } }
日志写入器
您可以使用我们的日志写入器来写入日志。
示例
<?php // global.php return [ 'log' => [ 'application.log' => [ 'writers' => [ [ 'name' => \Facile\SentryModule\Log\Writer\Sentry::class, 'options' => [ 'filters' => [ [ 'name' => 'priority', 'options' => [ 'priority' => \Laminas\Log\Logger::ERR, ], ], ], ], ], ] ], ], ];
用法
$logger->crit('Log this message'); // or with exceptions, to see the correct trace in sentry: $e = new \RuntimeException('test-exception'); $logger->crit($e->getMessage(), ['exception' => $e]);
JavaScript
此模块可以注入Raven客户端库,并为您进行配置。
<?php // facile-sentry.module.local.php return [ 'sentry' => [ 'javascript' => [ 'inject_script' => true, // enable it 'options' => [ 'dsn' => '', // other sentry options // https://docs.sentry.io/error-reporting/configuration/?platform=php ], // script options (defaults) 'script' => [ 'src' => 'https://#/5.6.3/bundle.min.js', 'integrity' => 'sha384-/Cqa/8kaWn7emdqIBLk3AkFMAHBk0LObErtMhO+hr52CntkaurEnihPmqYj3uJho', 'crossorigin' => 'anonymous', ], ], ], ];
在您的布局中
<?= $this->headScript() ?>
与Mezzio(例如zend-expressive)一起使用
如果您想与Mezzio一起使用它,您应初始化Sentry客户端和Hub。您可以通过简单地检索HubInterface服务来初始化它。
use Sentry\HubInterface; $container->get(HubInterface::class);