diff --git a/README.md b/README.md index 533aeb25..9c011fbd 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/Rollbar.php b/src/Rollbar.php index 2fceef83..b941dcda 100644 --- a/src/Rollbar.php +++ b/src/Rollbar.php @@ -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(); } @@ -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; diff --git a/tests/RollbarTest.php b/tests/RollbarTest.php index d2d7d673..bfbd48e0 100644 --- a/tests/RollbarTest.php +++ b/tests/RollbarTest.php @@ -7,6 +7,11 @@ 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 { @@ -14,14 +19,67 @@ class RollbarTest extends \PHPUnit_Framework_TestCase '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) { @@ -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', @@ -84,6 +146,8 @@ public function testBackwardsSimpleException() public function testBackwardsFlush() { + Rollbar::init(self::$simpleConfig); + Rollbar::flush(); $this->assertTrue(true); }