NEW Add getLogger() to MonologErrorHandler and add test for exception without one

This commit is contained in:
Robbie Averill 2018-10-20 15:00:08 +02:00
parent 5bd05a2deb
commit 3cdb73bd44
2 changed files with 33 additions and 2 deletions

View File

@ -16,19 +16,31 @@ class MonologErrorHandler implements ErrorHandler
* Set the PSR-3 logger to send errors & exceptions to
*
* @param LoggerInterface $logger
* @return $this
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
return $this;
}
/**
* Get the PSR-3 logger to send errors & exceptions to
*
* @return LoggerInterface
*/
public function getLogger()
{
return $this->logger;
}
public function start()
{
if (!$this->logger) {
if (!$this->getLogger()) {
throw new \InvalidArgumentException("No Logger property passed to MonologErrorHandler."
. "Is your Injector config correct?");
}
MonologHandler::register($this->logger);
MonologHandler::register($this->getLogger());
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace SilverStripe\Logging\Tests;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Logging\MonologErrorHandler;
class MonologErrorHandlerTest extends SapphireTest
{
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessageRegExp /No Logger property passed to MonologErrorHandler/
*/
public function testStartThrowsExceptionWithoutLoggerDefined()
{
$handler = new MonologErrorHandler();
$handler->start();
}
}