1

I am using rollbar.com to collect all details about exceptions in symfony2 app. However I don't understand how can I configure monolog so it would pass username and user id to rollbar.

I see that I can pass rollbar config as shown here and I am thinking person_fn is what I need. Still I don't know where to put this function (this should be in service because I need to check security token) and how to pass it to rollbar.

# config_prod.yml
rollbar:
    type: rollbar
    level: error
    token: %rollbar_token%
    config:
        person_fn: getUserForRollbarRightAboutNowOrSomething
Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43
Aurelijus Rozenas
  • 2,176
  • 2
  • 28
  • 40

2 Answers2

3

Found solution:

  • update monolog/monolog bundle to at least 1.17.0 version.
  • create ContextProcessor and update user information

    #src/AppBundle/Monolog/RollbarContextProcessor
    
    namespace AppBundle\Monolog;
    
    use AppBundle\Entity\User;
    use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
    
    class RollbarContextProcessor
    {
        private $tokenStorage;
    
        public function __construct(TokenStorageInterface $tokenStorage)
        {
            $this->tokenStorage = $tokenStorage;
        }
    
        public function processRecord($record)
        {
            if ($this->tokenStorage->getToken()) {
                $user = $this->tokenStorage->getToken()->getUser();
    
                if ($user instanceof User) {
                    $record['context']['payload']['person'] = [
                        'id' => $user->getId(),
                        'username' => $user->getUsername(),
                        'email' => $user->getEmail(),
                    ];
                }
            }
    
            return $record;
        }
    }
    
  • configure ContextProcessor as service with monolog.processor tag.

    # app/config/config_prod.yml
    services:
        monolog.processor.rollbar_context:
            class: AppBundle\Monolog\RollbarContextProcessor
            arguments:  [@security.token_storage]
            tags:
                - { name: monolog.processor, method: processRecord, handler: rollbar }
    monolog:
        handlers:
            rollbar:
                type: rollbar
                level: error
                token: %rollbar_token%
    
Aurelijus Rozenas
  • 2,176
  • 2
  • 28
  • 40
1

Your question has two parts:

Rollbar

person_fn is exactly what you need. You should be able to add a reference to the function by using a string (e.g.: "MyClass::static_function_reference" or "my_function_name").

Symfony

Disclaimer: I don't use or know much about Symfony.

This question has some excellent examples of how to get the current user in Symfony. (Punch line: in a controller you can call $this.getUser())

This question has a good example of how to inject the current user in a service. (Make a Twig Extension that depends on the SecurityContext or TokenStorage, use those dependencies to get a user objet).

Finally, there's the classic PHP move: as soon as you have a user add it to $_REQUEST. I'm not sure if Symfony co-opts this, but it'd be a valid way in a non-framework PHP application.

Community
  • 1
  • 1
Chris Pfohl
  • 18,220
  • 9
  • 68
  • 111