rollbar / rollbar-php-symfony-bundle

Bundle for Symfony that integrates Rollbar tracker
MIT License
28 stars 25 forks source link

Possibility to use service as person_fn rather than a static function #52

Open benjamindulau opened 5 years ago

benjamindulau commented 5 years ago

We can only use a static function for person_fn which is not really useful since most of the time we'd want to access some dependencies from inside the function and thus use a service instead of a static.

In older versions of Rollbar, we were able to add person data from inside a monolog.processor tagged service. Like that:

class RollbarContextProcessor
{
    /** @var TokenStorageInterface */
    protected $tokenStorageInterface;

    public function __construct(TokenStorageInterface $tokenStorageInterface)
    {
        $this->tokenStorageInterface = $tokenStorageInterface;
    }    

    public function processRecord(array $record): array
    {
        $record = $this->recordUser($record);
    }

    private function recordUser(array $record): array
    {
        if (!\is_object($this->tokenStorageInterface->getToken())) {
            return $record;
        }

        /** @var User $user */
        $user = $this->tokenStorageInterface->getToken()->getUser();

        if ($user instanceof User) {
            $record['context']['payload']['person'] = [
                'id'         => $user->getId(),
                'username'   => $user->getUsername(),
                'facebookId' => $user->getFacebookId(),
                'roles'      => implode('|', $user->getRoles())
            ];
        }

        return $record;
    }
}

But as of now, Rollbar doesn't consider this as being person data (like before) as it is added in body.extra variables.

armetiz commented 4 years ago

any news ?

kwolniak commented 1 year ago

Guys help me, is there any way to access logged in user in person_fn static function or is it just useless?