guave/sentry-bundle

适用于 Contao 4.9+ 的 Sentry 集成

安装: 376

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 0

开放问题: 0

类型:contao-bundle

1.0.2 2024-07-12 06:18 UTC

This package is auto-updated.

Last update: 2024-09-12 06:41:42 UTC


README

此 Contao 模块允许您通过 Twig 将 Sentry 集成到模板中,使用 sentry/sentry-symfony

需求

  • Contao 4.13+
  • PHP 7.4 或 8.0+

安装

$ composer require guave/sentry-bundle

使用方法

首先,配置 sentry/sentry-symfony 包:文档

推荐配置

sentry:
    dsn: "https://xyz@sentry.io/xy"
    register_error_listener: false

Sentry\Monolog\Handler:
    arguments:
        $hub: '@Sentry\State\HubInterface'
        $level: !php/const Monolog\Logger::ERROR # Can be one of https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#log-levels, but System::log() only uses INFO or ERROR
        $bubble: false

monolog:
    handlers:
        sentry:
            type: service
            id: Sentry\Monolog\Handler
            priority: 100 # Higher priority than ContaoTableHandler which will stop handling afterwards (bubbling is set to true)
            bubble: false # Use bubble: true if you don't want the logs to show up in the system log (bubbling means, no monolog handlers will run afterwards)

用户反馈

另一方面,您可能想实现 Sentry 的 用户反馈 功能。用户反馈主要是有用的,可以让用户知道您已经通知了问题,并让用户有机会添加一些评论。

为了集成此功能,您需要修改错误页面模板。将 vendor/contao/core-bundle/src/Resources/views/Error/layout.html.twig 的副本放置在 app/Resources/ContaoCoreBundle/views/Error/ 目录中。

修改复制的模板,并将以下代码片段放在 </body> 标签之前

{% set sentry_id = ''|sentry_last_event_id %}
{% if sentry_id %}
    <script src="https://#/5.7.1/bundle.min.js"
            integrity="sha384-KMv6bBTABABhv0NI+rVWly6PIRvdippFEgjpKyxUcpEmDWZTkDOiueL5xW+cztZZ"
            crossorigin="anonymous"></script>
    <script>
        Sentry.init({dsn: '{{ ''|sentry_dsn }}'});
        Sentry.showReportDialog({eventId: '{{ sentry_id }}'})

        // You can also bind the "show" method to an event, e.g. to open the modal on button click
        {#document.querySelector('.btn-report').addEventListener('click', function (e) {#}
        {#    e.preventDefault();#}
        {#    Sentry.showReportDialog({eventId: '{{ sentry_id }}'})#}
        {#});#}
    </script>
{% endif %}