facile-it / sentry-module
此模块允许将Sentry客户端集成到laminas和mezzio中
4.0.0
2024-05-17 14:59 UTC
Requires
- php: ^7.4 || ^8.0
- ext-json: *
- laminas/laminas-servicemanager: ^3.4.0 || ^4.0
- nyholm/psr7: ^1.8
- sentry/sentry: ^4.0.0
Requires (Dev)
- facile-it/facile-coding-standard: ^1.2.0
- friendsofphp/php-cs-fixer: ^3.0
- laminas/laminas-eventmanager: ^3.2.1 || ^4.0
- 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-phpunit: ^2.0
- phpunit/phpunit: ^9.0
- vimeo/psalm: ^5.0
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
This package is auto-updated.
Last update: 2024-09-17 15:41:27 UTC
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
此模块可以注入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);