简体   繁体   中英

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.

This is what I have in 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. 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.

I removed the extra '/' here:

Wrong:

http://KEY@URL//PROJECTID

Correct:

http://KEY@URL/PROJECTID

Kind of a stupid mistake, but thanks for the help anyway guys.

Maybe your problem is dsn . But I used the easiest way in the project You can find a more efficient way

see here more #sentry integrations

$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);

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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