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