facile-it/sentry-module

此模块允许将Sentry客户端集成到laminas和mezzio中

4.0.0 2024-05-17 14:59 UTC

README

此模块允许将Sentry客户端集成到laminas和mezzio。

安装

安装此模块的唯一支持方式是通过composer。有关composer文档,您可以参考 getcomposer.org

使用 php-http/async-client-implementationpsr/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_ERRORMvcEvent::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);