rollbar-php-symfony-bundle icon indicating copy to clipboard operation
rollbar-php-symfony-bundle copied to clipboard

Possibility to use service as person_fn rather than a static function

Open benjamindulau opened this issue 5 years ago • 2 comments

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.

benjamindulau avatar Jul 31 '19 12:07 benjamindulau

any news ?

armetiz avatar May 29 '20 06:05 armetiz

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

kwolniak avatar Mar 02 '23 13:03 kwolniak