如何将用户添加到

如何将用户添加到

本文介绍了如何将用户添加到 Symfony4 中的 Sentry 以处理所有异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Sentry 添加到我的项目中,但它不会为某些事件添加当前登录的用户.

I added Sentry to my project, but it doesn't add the currently logged in user for some events.

我添加了一个事件订阅者,但我不确定我是否真的需要它.对于像 ArgumentCountError 这样的异常,一切正常.对于 NotFoundHttpException 它没有.经过一些调试后,我发现 ExceptionListener->onKernelRequest 不会在每个异常中都被调用.我已经清空了 sentry.yaml 中的 skip_capture,但这没有帮助.我错过了什么.我的猜测是传播在某处停止.但是哪里?我该如何改变?

I added an event subscriber, but I'm not sure anymore if I really need it. For excepctions like ArgumentCountError everything works fine. For NotFoundHttpException it doesn't. After a bit of debugging I found, that ExceptionListener->onKernelRequest isn't called with every exception. I already emptied skip_capture in sentry.yaml, but this didn't help.What am I missing. My guess would be that propagation is stopped somewhere. But where? And how can I change this?

<?php
// src/EventSubscriber/SentrySubscriber

namespace App\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Sentry\SentryBundle\SentrySymfonyEvents;
use Symfony\Component\HttpKernel\KernelEvents;

class SentrySubscriber implements EventSubscriberInterface {

    /** @var \Raven_Client */
    protected $client;
    private $username;

    public function __construct(\Raven_Client $client) {
        $this->client = $client;
    }

    public static function getSubscribedEvents() {
        return array(
            SentrySymfonyEvents::PRE_CAPTURE => 'preCapture',
            SentrySymfonyEvents::SET_USER_CONTEXT => 'setUserContext',
        );
    }

    public function preCapture(GetResponseForExceptionEvent $event, $eventName, EventDispatcher $dispatcher) {
// username is empty, because setUserContext wasn't executed
// echo 'preCapture';var_dump($this->username);var_dump($this->client->_user);
        $this->client->user_context(['username' => $this->username]);
    }

    public function setUserContext(\Sentry\SentryBundle\Event\SentryUserContextEvent $event) {
//      echo 'setUserContext';      var_dump($event->getAuthenticationToken()->getUsername());
        $this->username = $event->getAuthenticationToken()->getUsername();
    }

}

我的配置:

// config/services.yaml
services:
    App\EventSubscriber\SentrySubscriber:
        arguments:
          - '@sentry.client'
        tags:
          - { name: kernel.event_subscriber }

// config/packages/sentry.yaml
sentry:
    dsn: '%env(SENTRY_DSN)%'
    options:
        curl_method: async
        release: '%env(RELEASE)%'

    skip_capture: # nothing else here, because everything should be captured

推荐答案

Sentry 默认不发送用户数据,以防止出现 GDPR 和类似法规的问题.

Sentry doesn't send user data by default to prevent issues with GDPR and similar regulations.

如果要重新启用发送用户数据,请将其添加到 sentry.yaml:

If you want to re-enable sending user data, add this to sentry.yaml:

sentry:
    options:
        send_default_pii: true

官方文档此处.

检查在\Sentry\SentryBundle\EventListener\RequestListener::onKernelRequest中实现.

这篇关于如何将用户添加到 Symfony4 中的 Sentry 以处理所有异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 11:47