From e2b57b173439441b9dcf058415571696b8605e02 Mon Sep 17 00:00:00 2001 From: Richard van Laak Date: Tue, 11 Feb 2020 16:34:30 +0100 Subject: [PATCH 1/3] support Monolog 2 --- composer.json | 2 +- src/Micrometa/Infrastructure/Logger/ExceptionLogger.php | 7 +++---- .../{LoggerTest.php => ExceptionLoggerTest.php} | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) rename src/Micrometa/Tests/Infrastructure/{LoggerTest.php => ExceptionLoggerTest.php} (98%) diff --git a/composer.json b/composer.json index 78f02bf..2c4a384 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" diff --git a/src/Micrometa/Infrastructure/Logger/ExceptionLogger.php b/src/Micrometa/Infrastructure/Logger/ExceptionLogger.php index e3a1eed..e5acb7d 100644 --- a/src/Micrometa/Infrastructure/Logger/ExceptionLogger.php +++ b/src/Micrometa/Infrastructure/Logger/ExceptionLogger.php @@ -71,17 +71,16 @@ 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 = []): void { + $this->addRecord($level, $message, $context); + if ($this->isTriggered($level)) { throw $this->getContextException($context) ?: new RuntimeException($message, $level); } - - return parent::addRecord($level, $message, $context); } /** diff --git a/src/Micrometa/Tests/Infrastructure/LoggerTest.php b/src/Micrometa/Tests/Infrastructure/ExceptionLoggerTest.php similarity index 98% rename from src/Micrometa/Tests/Infrastructure/LoggerTest.php rename to src/Micrometa/Tests/Infrastructure/ExceptionLoggerTest.php index d70ed04..ffabecb 100644 --- a/src/Micrometa/Tests/Infrastructure/LoggerTest.php +++ b/src/Micrometa/Tests/Infrastructure/ExceptionLoggerTest.php @@ -45,7 +45,7 @@ * @package Jkphl\Micrometa * @subpackage Jkphl\Micrometa\Tests */ -class LoggerTest extends AbstractTestBase +class ExceptionLoggerTest extends AbstractTestBase { /** * Test the exception logger From ab6df7cf537b72b48763a0aeda486c7c346daf63 Mon Sep 17 00:00:00 2001 From: Richard van Laak Date: Tue, 11 Feb 2020 17:18:06 +0100 Subject: [PATCH 2/3] support Monolog 2 --- src/Micrometa/Infrastructure/Logger/ExceptionLogger.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Micrometa/Infrastructure/Logger/ExceptionLogger.php b/src/Micrometa/Infrastructure/Logger/ExceptionLogger.php index e5acb7d..67a1001 100644 --- a/src/Micrometa/Infrastructure/Logger/ExceptionLogger.php +++ b/src/Micrometa/Infrastructure/Logger/ExceptionLogger.php @@ -74,13 +74,15 @@ public function __construct($threshold = Logger::ERROR) * @throws \Exception Exception that occured * @throws \RuntimeException Log message as exception */ - public function log($level, $message, array $context = []): void + public function addRecord($level, $message, array $context = []) { - $this->addRecord($level, $message, $context); + $processed = parent::addRecord($level, $message, $context); if ($this->isTriggered($level)) { throw $this->getContextException($context) ?: new RuntimeException($message, $level); } + + return $processed; } /** From 27fef45f359a5c4d8edbb02825caeb3c9aa46f07 Mon Sep 17 00:00:00 2001 From: Richard van Laak Date: Tue, 11 Feb 2020 17:39:47 +0100 Subject: [PATCH 3/3] composition over inheritance to support higher PHP versions while preserving PSR compatibility --- .../Infrastructure/Logger/ExceptionLogger.php | 22 ++++++++++++++----- .../Infrastructure/ExceptionLoggerTest.php | 9 -------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/Micrometa/Infrastructure/Logger/ExceptionLogger.php b/src/Micrometa/Infrastructure/Logger/ExceptionLogger.php index 67a1001..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()]); } /** @@ -74,15 +81,15 @@ public function __construct($threshold = Logger::ERROR) * @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 = []) { - $processed = parent::addRecord($level, $message, $context); + $level = Logger::toMonologLevel($level); if ($this->isTriggered($level)) { throw $this->getContextException($context) ?: new RuntimeException($message, $level); } - return $processed; + $this->decoratedLogger->addRecord($level, $message, $context); } /** @@ -109,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/ExceptionLoggerTest.php b/src/Micrometa/Tests/Infrastructure/ExceptionLoggerTest.php index ffabecb..da8a59c 100644 --- a/src/Micrometa/Tests/Infrastructure/ExceptionLoggerTest.php +++ b/src/Micrometa/Tests/Infrastructure/ExceptionLoggerTest.php @@ -47,15 +47,6 @@ */ class ExceptionLoggerTest extends AbstractTestBase { - /** - * Test the exception logger - */ - public function testExceptionLogger() - { - $logger = new ExceptionLogger(); - $this->assertTrue($logger->debug('DEBUG')); - } - /** * Test the exception logger *