Skip to content

Commit

Permalink
Changed register behaviour and small tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Mar 20, 2016
1 parent 0f6f945 commit a75052c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 102 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require-dev": {
"orchestra/testbench": "^3.0",
"mockery/mockery": "^0.9",
"satooshi/php-coveralls": "^0.6"
"satooshi/php-coveralls": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
101 changes: 38 additions & 63 deletions src/RavenLogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

use Exception;
use Illuminate\Foundation\Application;
use InvalidArgumentException;
use Monolog\Logger as Monolog;
use Psr\Log\AbstractLogger;
use Raven_Client;

class RavenLogHandler {

class RavenLogHandler extends AbstractLogger
{
/**
* The raven client instance.
*
Expand All @@ -28,6 +30,23 @@ class RavenLogHandler {
*/
protected $level;

/**
* The Log levels.
*
* @var array
*/
protected $levels = [
'debug' => Monolog::DEBUG,
'info' => Monolog::INFO,
'notice' => Monolog::NOTICE,
'warning' => Monolog::WARNING,
'error' => Monolog::ERROR,
'critical' => Monolog::CRITICAL,
'alert' => Monolog::ALERT,
'emergency' => Monolog::EMERGENCY,
'none' => 1000,
];

/**
* Constructor.
*/
Expand All @@ -50,21 +69,17 @@ public function __construct(Raven_Client $raven, Application $app, $level = 'deb
public function log($level, $message, array $context = [])
{
// Check if we want to log this message.
if ($this->parseLevel($level) < $this->level)
{
if ($this->parseLevel($level) < $this->level) {
return;
}

// Put level in context.
$context['level'] = $level;
$context = $this->addContext($context);

if ($message instanceof Exception)
{
if ($message instanceof Exception) {
$this->raven->captureException($message, $context);
}
else
{
} else {
$this->raven->captureMessage($message, [], $context);
}
}
Expand All @@ -77,25 +92,19 @@ public function log($level, $message, array $context = [])
protected function addContext(array $context = [])
{
// Add session data.
if ($session = $this->app->session->all())
{
if (empty($context['user']) or ! is_array($context['user']))
{
if ($session = $this->app->session->all()) {
if (empty($context['user']) or ! is_array($context['user'])) {
$context['user'] = [];
}

if (isset($context['user']['data']))
{
if (isset($context['user']['data'])) {
$context['user']['data'] = array_merge($session, $context['user']['data']);
}
else
{
} else {
$context['user']['data'] = $session;
}

// User session id as user id if not set.
if (! isset($context['user']['id']))
{
if (! isset($context['user']['id'])) {
$context['user']['id'] = $this->app->session->getId();
}
}
Expand All @@ -107,12 +116,9 @@ protected function addContext(array $context = [])
];

// Add tags to context.
if (isset($context['tags']))
{
if (isset($context['tags'])) {
$context['tags'] = array_merge($tags, $context['tags']);
}
else
{
} else {
$context['tags'] = $tags;
}

Expand All @@ -126,12 +132,9 @@ protected function addContext(array $context = [])
$extra = array_merge($extra, array_except($context, ['user', 'tags', 'level', 'extra']));

// Add extra to context.
if (isset($context['extra']))
{
if (isset($context['extra'])) {
$context['extra'] = array_merge($extra, $context['extra']);
}
else
{
} else {
$context['extra'] = $extra;
}

Expand All @@ -144,45 +147,17 @@ protected function addContext(array $context = [])
/**
* Parse the string level into a Monolog constant.
*
* @param string $level
* @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.");
if (isset($this->levels[$level])) {
return $this->levels[$level];
}
}

throw new InvalidArgumentException('Invalid log level: ' . $level);
}
}
35 changes: 16 additions & 19 deletions src/RavenServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Raven_Client;
use Raven_ErrorHandler;

class RavenServiceProvider extends ServiceProvider {

class RavenServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
Expand All @@ -26,8 +26,7 @@ class RavenServiceProvider extends ServiceProvider {
*/
public function boot()
{
if ( ! $this->listenerRegistered)
{
if (! $this->listenerRegistered) {
$this->registerListener();
}
}
Expand All @@ -37,10 +36,15 @@ public function boot()
*/
public function register()
{
// Don't register rollbar if it is not configured.
if (! getenv('RAVEN_DSN') and ! $this->app['config']->get('services.raven')) {
return;
}

$app = $this->app;

$this->app['Raven_Client'] = $this->app->share(function ($app)
{
$this->app['Raven_Client'] = $this->app->share(function ($app) {
// Default configuration.
$defaults = [
'curl_method' => 'async',
];
Expand All @@ -49,31 +53,26 @@ public function register()

$dsn = getenv('RAVEN_DSN') ?: $app['config']->get('services.raven.dsn');

if (! $dsn)
{
if (! $dsn) {
throw new InvalidArgumentException('Raven DSN not configured');
}

return new Raven_Client($dsn, array_except($config, ['dsn']));
});

$this->app['Jenssegers\Raven\RavenLogHandler'] = $this->app->share(function ($app)
{
$this->app['Jenssegers\Raven\RavenLogHandler'] = $this->app->share(function ($app) {
$level = getenv('RAVEN_LEVEL') ?: $app['config']->get('services.raven.level', 'debug');

return new RavenLogHandler($app['Raven_Client'], $app, $level);
});

if (isset($this->app['log']))
{
if (isset($this->app['log'])) {
$this->registerListener();
}

// Register the fatal error handler.
register_shutdown_function(function () use ($app)
{
if (isset($app['Raven_Client']))
{
register_shutdown_function(function () use ($app) {
if (isset($app['Raven_Client'])) {
(new Raven_ErrorHandler($app['Raven_Client']))->registerShutdownFunction();
}
});
Expand All @@ -86,12 +85,10 @@ protected function registerListener()
{
$app = $this->app;

$this->app['log']->listen(function ($level, $message, $context) use ($app)
{
$this->app['log']->listen(function ($level, $message, $context) use ($app) {
$app['Jenssegers\Raven\RavenLogHandler']->log($level, $message, $context);
});

$this->listenerRegistered = true;
}

}
23 changes: 4 additions & 19 deletions tests/RavenTest.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
<?php

class RavenTest extends Orchestra\Testbench\TestCase {

class RavenTest extends Orchestra\Testbench\TestCase
{
public function setUp()
{
parent::setUp();

$this->dsn = 'https://foo:bar@app.getsentry.com/12345';
$this->app->config->set('services.raven.dsn', $this->dsn);
}
putenv('RAVEN_DSN=' . $this->dsn);

public function tearDown()
{
Mockery::close();
parent::tearDown();
parent::setUp();
}

protected function getPackageProviders($app)
Expand Down Expand Up @@ -43,14 +37,6 @@ public function testFacade()
$this->assertInstanceOf('Raven_Client', $client);
}

public function testNoConfiguration()
{
$this->setExpectedException('InvalidArgumentException');

$this->app->config->set('services.raven.dsn', null);
$client = $this->app->make('Raven_Client');
}

public function testPassConfiguration()
{
$client = $this->app->make('Raven_Client');
Expand Down Expand Up @@ -225,5 +211,4 @@ public function testAboveLevel()
$this->app->log->alert('hello');
$this->app->log->emergency('hello');
}

}

0 comments on commit a75052c

Please sign in to comment.