oneup / contao-sentry-bundle
此扩展包为 Contao 4.4.x 及更高版本提供了一个轻松集成 sentry.io 的方法。
Requires
- php: ^8.1
- contao/core-bundle: ^4.13 || ^5.0
- sentry/sentry-symfony: ^5.0
- twig/twig: ^2.7 || ^3.0
Requires (Dev)
- contao/manager-plugin: ^2.0
- friendsofphp/php-cs-fixer: ^2.13
- phpstan/phpstan: ^1.10
- sentry/sentry: ^3.0 || ^4.0
- symfony/config: ^4.0 || ^5.0 || ^6.0
- symfony/dependency-injection: ^4.0 || ^5.0 || ^6.0
- symfony/http-kernel: ^4.0 || ^5.0 || ^6.0
README
此 Contao 扩展包为 Contao 4.13 和 5.x 版本提供了一个轻松集成 sentry.io 的方法。
--
这是一个针对 sentry/sentry-symfony 扩展包 的 "包装扩展"。
在 Contao 管理版中设置
基本集成已自动配置为一些合理的默认值。要启用集成,请在您的 .env.local 文件中配置 SENTRY_DSN 变量。
此外,您还可以在 .env.local 文件中命名 SENTRY_ENV,这在您有 test 和 prod 安装的情况下可能很有用。
如果您需要更改任何默认设置,只需根据 文档 配置 sentry/sentry-symfony 扩展包即可。
手动配置
如果您不使用 Contao 管理版,则需要像配置 sentry/sentry-symfony 扩展包一样配置此扩展包: 文档
用户反馈
另一方面,您可能想实现 sentry 的 用户反馈 功能。用户反馈主要用于让用户知道您已收到有关问题的通知,并让用户有机会添加一些评论。
为了集成此功能,您必须修改错误页面模板。将 vendor/contao/core-bundle/src/Resources/views/Error/layout.html.twig 的副本放置在 templates/ContaoCoreBundle/views/Error/ 目录中。
修改复制的模板,并在 </body> 标签之前放置以下代码片段
{% set sentry_id = ''|sentry_last_event_id %}
{% if sentry_id %}
<script src="https://#/8.7.0/bundle.feedback.min.js"
integrity="sha384-If5t0OtWMly236c4qvXxYalt8pOLHOj9qKZaXu/xDqMqJ5xmdMCwVwXP6dlPyALI"
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 %}
错误跟踪助手
Oneup\ContaoSentryBundle\ErrorHandlingTrait 添加了一些有用的 Sentry 辅助工具。
-
ErrorHandlingTrait::sentryOrThrow将将错误/异常记录到 sentry,或者在没有 Sentry 集成(例如在本地主机或dev环境中)的情况下抛出异常。这在运行循环 cronjobs 时非常有用,例如同步 Contao 与远程系统,这样在同步记录时发生的错误不会阻止同步循环完成其他记录。 -
ErrorHandlingTraig::sentryCheckIn已添加用于新的 Sentry Cron job 监控。不带参数调用sentryCheckIn()以开始检查,然后在作业成功运行或失败后,带布尔值true或false。
