Skip to content

Commit

Permalink
Add styleci
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed May 9, 2015
1 parent 924277d commit ed6d75b
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 162 deletions.
16 changes: 16 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
preset: psr2

enabled:
- duplicate_semicolon
- empty_return
- extra_empty_lines
- operators_spaces
- phpdoc_indent
- remove_leading_slash_use
- spaces_cast
- ternary_spaces
- unused_use

disabled:
- braces
- parenthesis
301 changes: 151 additions & 150 deletions src/RollbarLogHandler.php
Original file line number Diff line number Diff line change
@@ -1,159 +1,160 @@
<?php namespace Jenssegers\Rollbar;

use Exception, RollbarNotifier;
use Exception;
use RollbarNotifier;
use Monolog\Logger as Monolog;
use Illuminate\Foundation\Application;

class RollbarLogHandler {

/**
* The rollbar client instance.
*
* @var RollbarNotifier
*/
protected $rollbar;

/**
* The Laravel application.
*
* @var \Illuminate\Foundation\Application
*/
protected $app;

/**
* The minimum log level at which messages are sent to Rollbar.
*
* @var string
*/
protected $level;

/**
* Constructor.
*/
public function __construct(RollbarNotifier $rollbar, Application $app, $level = 'debug')
{
$this->rollbar = $rollbar;

$this->app = $app;

$this->level = $this->parseLevel($level ?: 'debug');

// Set Laravel information
$this->rollbar->environment = $this->app->environment();
$this->rollbar->root = base_path();
}

/**
* Log a message to Rollbar.
*
* @param mixed $level
* @param string $message
* @param array $context
*/
public function log($level, $message, array $context = array())
{
// Check if we want to log this message.
if ($this->parseLevel($level) < $this->level)
{
return;
}

$context = $this->addContext($context);

if ($message instanceof Exception)
{
$this->rollbar->report_exception($message);
}
else
{
$this->rollbar->report_message($message, $level, $context);
}
}

/**
* Add Laravel specific information to the context.
*
* @param array $context
*/
protected function addContext(array $context = array())
{
// Add session data.
if ($session = $this->app->session->all())
{
if (empty($this->rollbar->person) or ! is_array($this->rollbar->person))
{
$this->rollbar->person = [];
}

// Merge person context.
if (isset($context['person']) and is_array($context['person']))
{
$this->rollbar->person = $context['person'];
unset($context['person']);
}

// Add user session information.
if (isset($this->rollbar->person['session']))
{
$this->rollbar->person['session'] = array_merge($session, $this->rollbar->person['session']);
}
else
{
$this->rollbar->person['session'] = $session;
}

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

return $context;
}

/**
* Parse the string level into a Monolog constant.
*
* @param string $level
* @return int
*
* @throws \InvalidArgumentException
*/
protected function parseLevel($level)
{
switch ($level)
{
case 'debug':
return Monolog::DEBUG;

case 'info':
return Monolog::INFO;

case 'notice':
return Monolog::NOTICE;

case 'warning':
return Monolog::WARNING;

case 'error':
return Monolog::ERROR;

case 'critical':
return Monolog::CRITICAL;

case 'alert':
return Monolog::ALERT;

case 'emergency':
return Monolog::EMERGENCY;

case 'none':
return 1000;

default:
throw new \InvalidArgumentException("Invalid log level.");
}
}
/**
* The rollbar client instance.
*
* @var RollbarNotifier
*/
protected $rollbar;

/**
* The Laravel application.
*
* @var \Illuminate\Foundation\Application
*/
protected $app;

/**
* The minimum log level at which messages are sent to Rollbar.
*
* @var string
*/
protected $level;

/**
* Constructor.
*/
public function __construct(RollbarNotifier $rollbar, Application $app, $level = 'debug')
{
$this->rollbar = $rollbar;

$this->app = $app;

$this->level = $this->parseLevel($level ?: 'debug');

// Set Laravel information
$this->rollbar->environment = $this->app->environment();
$this->rollbar->root = base_path();
}

/**
* Log a message to Rollbar.
*
* @param mixed $level
* @param string $message
* @param array $context
*/
public function log($level, $message, array $context = array())
{
// Check if we want to log this message.
if ($this->parseLevel($level) < $this->level)
{
return;
}

$context = $this->addContext($context);

if ($message instanceof Exception)
{
$this->rollbar->report_exception($message);
}
else
{
$this->rollbar->report_message($message, $level, $context);
}
}

/**
* Add Laravel specific information to the context.
*
* @param array $context
*/
protected function addContext(array $context = array())
{
// Add session data.
if ($session = $this->app->session->all())
{
if (empty($this->rollbar->person) or ! is_array($this->rollbar->person))
{
$this->rollbar->person = [];
}

// Merge person context.
if (isset($context['person']) and is_array($context['person']))
{
$this->rollbar->person = $context['person'];
unset($context['person']);
}

// Add user session information.
if (isset($this->rollbar->person['session']))
{
$this->rollbar->person['session'] = array_merge($session, $this->rollbar->person['session']);
}
else
{
$this->rollbar->person['session'] = $session;
}

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

return $context;
}

/**
* Parse the string level into a Monolog constant.
*
* @param string $level
* @return int
*
* @throws \InvalidArgumentException
*/
protected function parseLevel($level)
{
switch ($level)
{
case 'debug':
return Monolog::DEBUG;

case 'info':
return Monolog::INFO;

case 'notice':
return Monolog::NOTICE;

case 'warning':
return Monolog::WARNING;

case 'error':
return Monolog::ERROR;

case 'critical':
return Monolog::CRITICAL;

case 'alert':
return Monolog::ALERT;

case 'emergency':
return Monolog::EMERGENCY;

case 'none':
return 1000;

default:
throw new \InvalidArgumentException("Invalid log level.");
}
}

}
13 changes: 7 additions & 6 deletions src/RollbarServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php namespace Jenssegers\Rollbar;

use Exception, InvalidArgumentException;
use RollbarNotifier, Rollbar;
use InvalidArgumentException;
use RollbarNotifier;
use Rollbar;
use Illuminate\Support\ServiceProvider;

class RollbarServiceProvider extends ServiceProvider {
Expand All @@ -23,13 +24,13 @@ public function boot()
$app = $this->app;

// Listen to log messages.
$app['log']->listen(function($level, $message, $context) use ($app)
$app['log']->listen(function ($level, $message, $context) use ($app)
{
$app['rollbar.handler']->log($level, $message, $context);
});

// Flush callback
$flush = function() use ($app)
$flush = function () use ($app)
{
if ($app->resolved('rollbar.client'))
{
Expand Down Expand Up @@ -61,7 +62,7 @@ public function register()
{
$app = $this->app;

$this->app['rollbar.client'] = $this->app->share(function($app)
$this->app['rollbar.client'] = $this->app->share(function ($app)
{
$config = $app['config']->get('services.rollbar');

Expand All @@ -75,7 +76,7 @@ public function register()
return $rollbar;
});

$this->app['rollbar.handler'] = $this->app->share(function($app)
$this->app['rollbar.handler'] = $this->app->share(function ($app)
{
$client = $app['rollbar.client'];

Expand Down
Loading

0 comments on commit ed6d75b

Please sign in to comment.