简体   繁体   English

使用 Monolog Logger 将输出发送到 Sentry

[英]Use Monolog Logger to send output to Sentry

I currently have php setup to write its error messages, exceptions, ... to stderr using Monolog and I wanted to add an additional Handler to send the output directly to Sentry.我目前有 php 设置来使用 Monolog 将其错误消息、异常...写入 stderr,我想添加一个额外的处理程序以将输出直接发送到 Sentry。

This is what I have in PHP:这是我在 PHP 中所拥有的:

$monologLogger = new Logger('logger');
$streamHandler = new StreamHandler('php://stderr');

$formatter = new JsonFormatter();

$options = [
    'dsn' => 'http://KEY@URL//PROJECTID',
    'default_integrations' => false, // use Monolog to send errors
];

\Sentry\init($options);
$sentryHandler = new Handler(SentrySdk::getCurrentHub(), Logger::ERROR);

$sentryHandler->setFormatter($formatter);
$monologLogger->pushHandler($sentryHandler);

$streamHandler->setFormatter($formatter);
$monologLogger->pushHandler($streamHandler);

return $monologLogger;

It outputs everything correctly to stderr, but I do not receive any events in sentry.它将所有内容正确输出到 stderr,但我在哨兵中没有收到任何事件。 Does anyone know what might be wrong with my script?有谁知道我的脚本可能有什么问题?

当您捕获异常时,您可以调用它以手动将其发送给 Sentry

Sentry\captureException($e);

使用try catch块获取异常,然后可以调用 Sentry

Sentry\\captureException($e);

I found the solution to my problem.我找到了解决我的问题的方法。 Even though I copied it directly from sentry, my DSN key was wrong.即使我直接从 sentry 复制它,我的 DSN 密钥也是错误的。

I removed the extra '/' here:我在这里删除了额外的“/”:

Wrong:错误的:

http://KEY@URL//PROJECTID http://KEY@URL//PROJECTID

Correct:正确的:

http://KEY@URL/PROJECTID http://KEY@URL/PROJECTID

Kind of a stupid mistake, but thanks for the help anyway guys.有点愚蠢的错误,但无论如何感谢您的帮助。

Maybe your problem is dsn .也许你的问题是dsn But I used the easiest way in the project You can find a more efficient way但是我在项目中使用了最简单的方法 可以找到更高效的方法

see here more #sentry integrations 在这里查看更多#sentry 集成

$logger = new \Monolog\Logger('name');
$client = \Sentry\ClientBuilder::create([
    'dsn' => DSN
])->getClient();

$handler = new \Sentry\Monolog\Handler(
   new \Sentry\State\Hub($client)
);

$logger->pushHandler($handler);
$logger->info('Message', $context);
$logger->error('Message', $context);

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM