diff --git a/composer.json b/composer.json index ac6ff31..130ca4e 100755 --- a/composer.json +++ b/composer.json @@ -34,7 +34,7 @@ "league/uri": "^5.0", "mf2/mf2": "^0.4", "ml/json-ld": "^1.1", - "monolog/monolog": "^1.24", + "monolog/monolog": "^1.24 || ^2", "psr/cache": "^1.0", "psr/log": "^1.1", "symfony/cache": "^4.0|^5.0" diff --git a/src/Micrometa/Infrastructure/Logger/ExceptionLogger.php b/src/Micrometa/Infrastructure/Logger/ExceptionLogger.php index e3a1eed..e586397 100644 --- a/src/Micrometa/Infrastructure/Logger/ExceptionLogger.php +++ b/src/Micrometa/Infrastructure/Logger/ExceptionLogger.php @@ -39,6 +39,9 @@ use Jkphl\Micrometa\Ports\Exceptions\RuntimeException; use Monolog\Handler\NullHandler; use Monolog\Logger; +use Monolog\ResettableInterface; +use Psr\Log\LoggerInterface; +use Psr\Log\LoggerTrait; /** * Exception logger @@ -46,8 +49,10 @@ * @package Jkphl\Micrometa * @subpackage Jkphl\Micrometa\Infrastructure */ -class ExceptionLogger extends Logger +final class ExceptionLogger implements LoggerInterface, ResettableInterface { + use LoggerTrait; + /** * Exception threshold * @@ -55,13 +60,15 @@ class ExceptionLogger extends Logger */ protected $threshold; + private $decoratedLogger; + /** * Constructor */ public function __construct($threshold = Logger::ERROR) { $this->threshold = $threshold; - parent::__construct('exception', [new NullHandler()]); + $this->decoratedLogger = new Logger('exception', [new NullHandler()]); } /** @@ -71,17 +78,18 @@ public function __construct($threshold = Logger::ERROR) * @param string $message The log message * @param array $context The log context * - * @return Boolean Whether the record has been processed * @throws \Exception Exception that occured * @throws \RuntimeException Log message as exception */ - public function addRecord($level, $message, array $context = []) + public function log($level, $message, array $context = []) { + $level = Logger::toMonologLevel($level); + if ($this->isTriggered($level)) { throw $this->getContextException($context) ?: new RuntimeException($message, $level); } - return parent::addRecord($level, $message, $context); + $this->decoratedLogger->addRecord($level, $message, $context); } /** @@ -108,4 +116,9 @@ protected function getContextException(array $context) return (isset($context['exception']) && ($context['exception'] instanceof \Exception)) ? $context['exception'] : null; } + + public function reset() + { + $this->decoratedLogger->reset(); + } } diff --git a/src/Micrometa/Tests/Infrastructure/LoggerTest.php b/src/Micrometa/Tests/Infrastructure/ExceptionLoggerTest.php similarity index 91% rename from src/Micrometa/Tests/Infrastructure/LoggerTest.php rename to src/Micrometa/Tests/Infrastructure/ExceptionLoggerTest.php index 19b9f6f..d692772 100644 --- a/src/Micrometa/Tests/Infrastructure/LoggerTest.php +++ b/src/Micrometa/Tests/Infrastructure/ExceptionLoggerTest.php @@ -45,17 +45,8 @@ * @package Jkphl\Micrometa * @subpackage Jkphl\Micrometa\Tests */ -class LoggerTest extends AbstractTestBase +class ExceptionLoggerTest extends AbstractTestBase { - /** - * Test the exception logger - */ - public function testExceptionLogger() - { - $logger = new ExceptionLogger(); - $this->assertTrue($logger->debug('DEBUG')); - } - /** * Test the exception logger */