codekandis / sentry-client
这个库代表了一个对 `Sentry SDK` 的包装,具有增强的开发特性。
Requires
- php: >=7.4
- codekandis/constants-classes-translator: ^1
- sentry/sdk: ^2
Requires (Dev)
- codekandis/phpunit: ^3
- roave/security-advisories: dev-master
This package is auto-updated.
Last update: 2024-09-27 18:14:38 UTC
README
codekandis/sentry-client
是 Sentry SDK 包 getsentry/sentry-php
的包装库,目前支持 Sentry SDK 版本 2.x
。它通过面向对象的 API 提供了被包装包的功能。
索引
安装
使用以下命令安装最新版本
$ composer require codekandis/sentry-client
如何使用
创建配置
有两种可能性。
使用默认配置
为了方便,默认配置 SentryClientConfiguration
实现了流畅的接口。
<?php declare( strict_types = 1 ); namespace Vendor\Project; use CodeKandis\SentryClient\Configurations\SentryClientConfiguration; $sentryClientConfiguration = ( new SentryClientConfiguration() ) ->setDsn( 'dsn' ) ->setErrorTypes( E_ALL ) ->setDisplayErrors( true );
在自己的项目中实现配置接口
SentryClient
带有配置接口 SentryClientConfigurationInterface
。因此,您可以自行实现配置。
<?php declare( strict_types = 1 ); namespace Vendor\Project; use CodeKandis\SentryClient\Configurations\SentryClientConfigurationInterface; class SentryClientConfiguration implements SentryClientConfigurationInterface { public function getDsn(): string { return 'dsn'; } /** * ... */ } $sentryClientConfiguration = new SentryClientConfiguration();
实例化 Sentry 客户端
<?php declare( strict_types = 1 ); namespace Vendor\Project; use CodeKandis\SentryClient\Configurations\SentryClientConfiguration; use CodeKandis\SentryClient\SentryClient; use const E_ALL; $sentryClient = new SentryClient( ( new SentryClientConfiguration() ) ->setDsn( 'dsn' ) ->setErrorTypes( E_ALL ) ->setDisplayErrors( true ) );
一旦 SentryClient
被实例化,PHP 指令 error_reporting
和 display_errors
将立即设置。请注意,一旦 display_errors
设置为 true
,将显示所有捕获的事件。所以除了错误和异常之外,还会显示手动捕获的消息。
捕获事件
有两种方法来捕获消息、错误和异常 - 在 Sentry 的术语中被称为事件。
手动捕获
消息
<?php declare( strict_types = 1 ); namespace Vendor\Project; use CodeKandis\SentryClient\Configurations\SentryClientConfiguration; use CodeKandis\SentryClient\SentryClient; use CodeKandis\SentryClient\Severities; ( new SentryClient( new SentryClientConfiguration() ) ) ->captureMessage( 'This is a message.', Severities::INFO, [ 'some' => 'context' ], [ 'some tag', 'another tag' ], [ 'id' => 'some username', 'ip_address' => '42.42.42.42' ] );
错误
只能捕获最后发生的错误。
<?php declare( strict_types = 1 ); namespace Vendor\Project; use CodeKandis\SentryClient\Configurations\SentryClientConfiguration; use CodeKandis\SentryClient\SentryClient; use CodeKandis\SentryClient\Severities; ( new SentryClient( new SentryClientConfiguration() ) ) ->captureLastError( Severities::ERROR, [ 'some' => 'context' ], [ 'some tag', 'another tag' ], [ 'id' => 'some username', 'ip_address' => '42.42.42.42' ] );
异常
<?php declare( strict_types = 1 ); namespace Vendor\Project; use CodeKandis\SentryClient\Configurations\SentryClientConfiguration; use CodeKandis\SentryClient\SentryClient; use CodeKandis\SentryClient\Severities; use Exception; ( new SentryClient( new SentryClientConfiguration() ) ) ->captureThrowable( new Exception( 'This is an exception' ), Severities::FATAL, [ 'some' => 'context' ], [ 'some tag', 'another tag' ], [ 'id' => 'some username', 'ip_address' => '42.42.42.42' ] );
自动捕获
SentryClient
带有内置的错误和异常处理器。一旦启用事件的自动捕获,发生的事件将由这些处理器捕获并发送到配置的 Sentry 实例。
要启用自动捕获,只需调用 SentryClient::register()
。
<?php declare( strict_types = 1 ); namespace Vendor\Project; use CodeKandis\SentryClient\Configurations\SentryClientConfiguration; use CodeKandis\SentryClient\SentryClient; ( new SentryClient( new SentryClientConfiguration() ) ) ->register();
使用自定义错误和异常处理器
如果您已经设置了您自己的错误或异常处理器,注册 SentryClient
进行自动捕获不会影响它们被执行。
<?php declare( strict_types = 1 ); namespace Vendor\Project; use CodeKandis\SentryClient\Configurations\SentryClientConfiguration; use CodeKandis\SentryClient\SentryClient; use Exception; use Throwable; use function set_error_handler; use function set_exception_handler; use function trigger_error; set_error_handler( function ( int $level, string $message ) { echo 'Error handler: ' . $message . "\n"; } ); set_exception_handler( function ( Throwable $exception ) { echo 'Exception handler: ' . $exception->getMessage() . "\n"; } ); ( new SentryClient( new SentryClientConfiguration() ) ) ->register(); trigger_error( 'An error occured.' ); // outputs `Error handler: An error occured.` throw new Exception( 'An exception occured.' ); // outputs `Exception handler: An exception occured.`
Sentry 客户端将其自己的处理器推送到其内部管理的处理器堆栈的顶部。因此,错误和异常处理器将按以下顺序执行
SentryClient
处理器- 包装的 Sentry SDK 处理器
- 您的自定义处理器
因此,可以保证所有事件都将发送到您的 Sentry 实例,并且您的处理器也将被执行。
测试
为了使集成测试运行,必须在 TestConstants
中进行一些设置。所有必要的信息都可以在您的Sentry实例中找到。
根据您的Sentry实例的工作负载,您的某些事件可能无法立即通过API获取。因此,必须设置一个适当的 TestConstants::EVENT_PROCESSING_THRESHOLD
(以秒为单位)。
由于封装的 Sentry SDK 的特性,一次性执行所有测试用例会导致某些集成测试失败。一些必要的测试输出会与后续测试混淆。建议手动逐个运行测试用例 SentryClientInterfaceTest
。