Skip to content

Commit 4a62239

Browse files
Merge pull request #12 from philbates35/person-function-fix
Fix person_fn not being called when session not empty
2 parents 75eabed + ddb7084 commit 4a62239

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)