gpt4 book ai didi

php - `sentry/sdk 2.x`-如何注册错误/异常处理

转载 作者:行者123 更新时间:2023-12-03 08:44:15 25 4
gpt4 key购买 nike

当我们一次使用多个哨兵实例时,我放弃了Sentry客户端的默认初始化,并设法手动创建了Sentry客户端。

$client = ClientBuilder::create(
[
'dsn' => $this->config->getDsn(),
'environment' => $this->config->getEnvironment(),
'release' => $this->config->getRelease(),
'error_types' => $this->config->getErrorReporting()
]
)->getClient();

现在,我可以手动捕获异常/错误或消息。

$client->captureException( ... );

但是我没有得到的是如何手动注册Sentry的异常/错误处理程序?

最佳答案

我们通过集成处理全局未处理的异常。
集成的工作原理有所不同,因为它们本质上是单例,并且需要能够保持全局状态,因为例如,我们不想在全局处理程序中多次挂接。

为了实现所需的功能,您需要直接使用Hub而不是Client

$client1 = Sentry\ClientBuilder::create([
'dsn' => 'DSN'
])->getClient();

$hub1 = new Sentry\State\Hub($client1);
// Making a Hub current, tells the global integrations to send stuff to the client registered in this Hub
Sentry\SentrySdk::setCurrentHub($hub1);

// This will be caught by and send by $client1
throw new Exception('DEMO TEST3');

$client2 = Sentry\ClientBuilder::create([
'dsn' => 'DSN'
])->getClient();

$hub2 = new Sentry\State\Hub($client2);
// Setting a new current hub here
Sentry\SentrySdk::setCurrentHub($hub2);

// This will be caught by and send by $client2
throw new Exception('DEMO TEST4');

因此一次只能有一个集线器可以成为当前的集线器,因此您的客户之一必须是处理全局异常的客户。
我希望这是有道理的。

关于php - `sentry/sdk 2.x`-如何注册错误/异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58184190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com