Skip to content

respect person_fn #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions src/RollbarLogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ public function __construct(RollbarLogger $logger, Application $app, $level = 'd
$this->level = $this->parseLevel($level ?: 'debug');
}

public function extendPersonWithSession(array $person)
{
$person['session'] = isset($person['session'])
? array_merge($this->app->session->all(), $person['session'])
: $this->app->session->all();

// User session id as user id if not set.
if (! isset($person['id'])) {
$person['id'] = $this->app->session->getId();
}

return $person;
}

/**
* Log a message to Rollbar.
*
Expand Down Expand Up @@ -86,27 +100,21 @@ public function log($level, $message, array $context = [])
protected function addContext(array $context = [])
{
// Add session data.
if ($session = $this->app->session->all()) {
if ($this->app->session->isStarted()) {
// Merge person context.
if (isset($context['person']) and is_array($context['person'])) {
$this->logger->configure(['person' => $context['person']]);
$person = $context['person'];
unset($context['person']);
} else {
// Add user session information.
$config = $this->logger->extend([]);
if (isset($config['person_fn'])) {
return $context;
}
$person = isset($config['person']) ? $config['person'] : [];
}

// Add user session information.
$config = $this->logger->extend([]);
$person = isset($config['person']) ? $config['person'] : [];

$person['session'] = isset($person['session']) ?
array_merge($session, $person['session']) :
$person['session'] = $session;

// User session id as user id if not set.
if (! isset($person['id'])) {
$person['id'] = $this->app->session->getId();
}

$this->logger->configure(['person' => $person]);
$this->logger->configure(['person' => $this->extendPersonWithSession($person)]);
}

return $context;
Expand Down
20 changes: 15 additions & 5 deletions src/RollbarServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,35 @@ public function register()
$app = $this->app;

$this->app->singleton('Rollbar\RollbarLogger', function ($app) {

$defaults = [
'environment' => $app->environment(),
'root' => base_path(),
];
$config = array_merge($defaults, $app['config']->get('services.rollbar', []));
$config['access_token'] = getenv('ROLLBAR_TOKEN') ?: $app['config']->get('services.rollbar.access_token');


if (isset($config['person_fn'])) {
$person_fn = $config['person_fn'];
$config['person_fn'] = function () use ($person_fn, $app) {
$person = call_user_func($person_fn) ?: [];
return $this->app->session->isStarted()
? $app['Rollbar\Laravel\RollbarLogHandler']->extendPersonWithSession($person)
: $person;
};
}

if (empty($config['access_token'])) {
throw new InvalidArgumentException('Rollbar access token not configured');
}

\Rollbar\Rollbar::init($config);

return Rollbar::logger();
});

$this->app->singleton('Rollbar\Laravel\RollbarLogHandler', function ($app) {

$level = getenv('ROLLBAR_LEVEL') ?: $app['config']->get('services.rollbar.level', 'debug');

return new RollbarLogHandler($app['Rollbar\RollbarLogger'], $app, $level);
Expand Down