Skip to content

Commit

Permalink
Merge pull request rollbar#190 from jasny/patch-1
Browse files Browse the repository at this point in the history
Decoupling of RollbarLogger in init
  • Loading branch information
ArturMoczulski authored Jun 11, 2017
2 parents da408fc + 231b42d commit 58b817e
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 6 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,23 @@ Rollbar::log(
?>
```

## Using dependency injection

If you're using dependency injection containers, you can create and get a `RollbarLogger` from the container and use it
to initialize Rollbar error logging.

It's up to the container to properly create and configure the logger.

```php
use Rollbar\Rollbar;
use Rollbar\RollbarLogger;

$logger = $container->get(RollbarLogger::class);

// installs global error and exception handlers
Rollbar::init($logger);
```

## Using Monolog

Here is an example of how to use Rollbar as a handler for Monolog:
Expand Down
25 changes: 20 additions & 5 deletions src/Rollbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ class Rollbar
private static $fatalErrors = array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR);

public static function init(
$config,
$configOrLogger,
$handleException = true,
$handleError = true,
$handleFatal = true
) {
if (is_null(self::$logger)) {
self::$logger = new RollbarLogger($config);
$setupHandlers = is_null(self::$logger);

self::setLogger($configOrLogger);

if ($setupHandlers) {
if ($handleException) {
self::setupExceptionHandling();
}
Expand All @@ -29,11 +31,24 @@ public static function init(
if ($handleFatal) {
self::setupFatalHandling();
}
} else {
self::$logger->configure($config);
}
}

private static function setLogger($configOrLogger)
{
if ($configOrLogger instanceof RollbarLogger) {
$logger = $configOrLogger;
}

// Replacing the logger rather than configuring the existing logger breaks BC
if (self::$logger && !isset($logger)) {
self::$logger->configure($configOrLogger);
return;
}

self::$logger = isset($logger) ? $logger : new RollbarLogger($configOrLogger);
}

public static function logger()
{
return self::$logger;
Expand Down
66 changes: 65 additions & 1 deletion tests/RollbarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,79 @@
use Rollbar\Rollbar;
use Rollbar\Payload\Level;

/**
* Usage of static method Rollbar::logger() is intended here.
*
* @SuppressWarnings(PHPMD.StaticAccess)
*/
class RollbarTest extends \PHPUnit_Framework_TestCase
{

private static $simpleConfig = array(
'access_token' => ROLLBAR_TEST_TOKEN,
'environment' => 'test'
);

private static function clearLogger()
{
$reflLoggerProperty = new \ReflectionProperty('Rollbar\Rollbar', 'logger');
$reflLoggerProperty->setAccessible(true);
$reflLoggerProperty->setValue(null);
}

public static function setupBeforeClass()
{
self::clearLogger();
}

public function tearDown()
{
self::clearLogger();
}

public function testInitWithConfig()
{
Rollbar::init(self::$simpleConfig);

$this->assertInstanceOf('Rollbar\RollbarLogger', Rollbar::logger());
$this->assertAttributeEquals(new Config(self::$simpleConfig), 'config', Rollbar::logger());
}

public function testInitWithLogger()
{
$logger = $this->getMockBuilder('Rollbar\RollbarLogger')->disableOriginalConstructor()->getMock();

Rollbar::init($logger);

$this->assertSame($logger, Rollbar::logger());
}

public function setUp()
public function testInitConfigureLogger()
{
$logger = $this->getMockBuilder('Rollbar\RollbarLogger')->disableOriginalConstructor()->getMock();
$logger->expects($this->once())->method('configure')->with(self::$simpleConfig);

Rollbar::init($logger);
Rollbar::init(self::$simpleConfig);
}

public function testInitReplaceLogger()
{
Rollbar::init(self::$simpleConfig);

$this->assertInstanceOf('Rollbar\RollbarLogger', Rollbar::logger());

$logger = $this->getMockBuilder('Rollbar\RollbarLogger')->disableOriginalConstructor()->getMock();

Rollbar::init($logger);

$this->assertSame($logger, Rollbar::logger());
}

public function testLogException()
{
Rollbar::init(self::$simpleConfig);

try {
throw new \Exception('test exception');
} catch (\Exception $e) {
Expand All @@ -33,12 +91,16 @@ public function testLogException()

public function testLogMessage()
{
Rollbar::init(self::$simpleConfig);

Rollbar::log(Level::info(), 'testing info level');
$this->assertTrue(true);
}

public function testLogExtraData()
{
Rollbar::init(self::$simpleConfig);

Rollbar::log(
Level::info(),
'testing extra data',
Expand Down Expand Up @@ -84,6 +146,8 @@ public function testBackwardsSimpleException()

public function testBackwardsFlush()
{
Rollbar::init(self::$simpleConfig);

Rollbar::flush();
$this->assertTrue(true);
}
Expand Down

0 comments on commit 58b817e

Please sign in to comment.