0

In configuring the Rollbar Monolog configuration, one of the options that can be set is the person_fn function. That configuration Rollbar is expecting to be some sort of "callable", which when called will return information about the user.

In order to get the information about the current user, that method is going to need the Session handler, to get the currently-logged-in user. I can write a service that does that:

<?php
class UserService {
  private $session;

  public function __construct(SessionInterface $session) {
    $this->session = $session;
  }

  public function RollbarUserFn() {
    $u = $this->session->get('current_user');
    return array(
      'id' => $u['id'],
      'username' => $u['username']
    ); 
  }
}

Now, if just using call_user_func, the RollbarUserFn method is not static (since it has a dependency), so cannot use "UserService::RollbarUserFn" (a string), but rather instantiate it and pass the object in:

$us = new UserService($sessionInterface);
call_user_func(array($us, 'RollbarUserFn'));

But how can I do that equivalent in the config YAML file? I've tried something like:

monolog:
  handlers:
    rollbar:
      type: rollbar
      token: '123123123'
      config:
        environment: '%env(APP_ENV)%'
        person_fn: [ '@UserService', 'RollbarUserFn' ]

But that throws an error that the person_fn config node is expected to be a Scalar, not an array

MidnightLightning
  • 6,715
  • 5
  • 44
  • 68
  • Hi there - The best way to get an answer to your question is to email support@rollbar.com. That'll get you directly in touch w/ the developers who maintain the Rollbar SDKs. – Jesse Gibbs Jun 19 '18 at 16:57
  • Thanks, @JesseGibbs, but the issue here isn't with Rollbar and its SDKs, but rather with Symfony and its means of describing "Services" that seems to be the sticking point. – MidnightLightning Jun 19 '18 at 20:00

1 Answers1

0

You can decorate RollbarHandlerFactory from rollbar/rollbar-php-symfony-bundle like that:

#config/services_prod.yaml

services:
  App\Service\RollbarHandlerFactory:
    decorates: Rollbar\Symfony\RollbarBundle\Factories\RollbarHandlerFactory
    arguments:
      - '@service_container'
      - ['password', 'plainPassword', 'proxyInitialized', 'proxyInitializer', 'photo']

and reconfigure logger

<?php

namespace App\Service;

use Rollbar\Rollbar;
use Rollbar\Symfony\RollbarBundle\Factories\RollbarHandlerFactory as BaseFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;

class RollbarHandlerFactory extends BaseFactory
{
    public function __construct(ContainerInterface $container, array $scrubFields = [])
    {
        parent::__construct($container);

        Rollbar::logger()->configure([
            'person_fn' => function () use ($container, $scrubFields) {
                try {
                    $token = $container->get('security.token_storage')->getToken();

                    if ($token) {
                        /** @var \App\Model\User $user */
                        $user = $token->getUser();
                        $serializer = $container->get('serializer');
                        $person = \json_decode($serializer->serialize($user, 'json'), true);

                        foreach ($scrubFields as $field) {
                            unset($person[$field]);
                        }

                        $person['id'] = $user->getEmail();

                        return $person;
                    }
                } catch (\Exception $exception) {
                    // Ignore
                }
            }
        ]);
    }
}