Skip to content

Commit ddb7084

Browse files
committed
Fix person_fn not being called when session not empty
Currently, when the session is not empty, this package always sets a "person" entry in the rollbar config, which results in the rollbar/rollbar package ever caling the person_fn. The rollbar/rollbar package only calls $config['person_fn'] when the $config['person'] doesn't already exist, which as stated above, will always be set when the session is not empty. Revert back to the behaviour originally found in RollbarLogHandler in 42920e5 before it was refactored in 8988c22 which introduced this bug. As 42920e5 used an older version of the rollbar/rollbar library, this commit doesn't revert the changes back exactly, but is the equivalent changes for the latest version of the rollbar/rollbar library API. Add a regression test that currently fails on master branch but passes with these canges to RollbarLogHandler.
1 parent 8aec936 commit ddb7084

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

src/RollbarLogHandler.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,33 @@ protected function addContext(array $context = [])
8787
{
8888
// Add session data.
8989
if ($session = $this->app->session->all()) {
90+
$config = $this->logger->extend([]);
91+
92+
if (empty($config['person']) or ! is_array($config['person'])) {
93+
$person = [];
94+
} else {
95+
$person = $config['person'];
96+
}
97+
9098
// Merge person context.
9199
if (isset($context['person']) and is_array($context['person'])) {
92-
$this->logger->configure(['person' => $context['person']]);
100+
$person = $context['person'];
93101
unset($context['person']);
102+
} else {
103+
if (isset($config['person_fn']) && is_callable($config['person_fn'])) {
104+
$data = @call_user_func($config['person_fn']);
105+
if (isset($data['id'])) {
106+
$person = call_user_func($config['person_fn']);
107+
}
108+
}
94109
}
95110

96111
// Add user session information.
97-
$config = $this->logger->extend([]);
98-
$person = isset($config['person']) ? $config['person'] : [];
99-
100-
$person['session'] = isset($person['session']) ?
101-
array_merge($session, $person['session']) :
112+
if (isset($person['session'])) {
113+
$person['session'] = array_merge($session, $person['session']);
114+
} else {
102115
$person['session'] = $session;
116+
}
103117

104118
// User session id as user id if not set.
105119
if (! isset($person['id'])) {

tests/RollbarTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,27 @@ public function testErrorLevels3()
177177
$this->app->log->alert('hello');
178178
$this->app->log->emergency('hello');
179179
}
180+
181+
public function testPersonFunctionIsCalledWhenSessionContainsAtLeastOneItem()
182+
{
183+
$this->app->config->set('services.rollbar.person_fn', function () {
184+
return [
185+
'id' => '123',
186+
'username' => 'joebloggs',
187+
];
188+
});
189+
190+
$logger = $this->app->make('Rollbar\RollbarLogger');
191+
192+
$this->app->session->put('foo', 'bar');
193+
194+
$this->app->log->debug('hello');
195+
196+
$config = $logger->extend([]);
197+
198+
$person = $config['person'];
199+
200+
$this->assertEquals('123', $person['id']);
201+
$this->assertEquals('joebloggs', $person['username']);
202+
}
180203
}

0 commit comments

Comments
 (0)