From 2a5076f8f7f4fba3c9e8fbf1789913966bababd9 Mon Sep 17 00:00:00 2001 From: Timon de Groot Date: Tue, 24 Oct 2023 13:41:44 +0200 Subject: [PATCH] Logging: Fixup logger for psr/log 3.x psr/log 3.x has added return types to all signatures, so that's why we need two different implementations of the same logger now. --- src/Configuration.php | 4 +-- src/Logging/LegacyLogger.php | 52 ++++++++++++++++++++++++++++++++++ src/Logging/LoggingFactory.php | 19 +++++++++++++ src/Logging/SimpleLogger.php | 17 +++++++---- 4 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 src/Logging/LegacyLogger.php create mode 100644 src/Logging/LoggingFactory.php diff --git a/src/Configuration.php b/src/Configuration.php index 632d0ea..747dd96 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -2,7 +2,7 @@ namespace Hypernode\DeployConfiguration; -use Hypernode\DeployConfiguration\Logging\SimpleLogger; +use Hypernode\DeployConfiguration\Logging\LoggingFactory; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; @@ -157,7 +157,7 @@ class Configuration public function __construct() { - $this->logger = new SimpleLogger(LogLevel::INFO); + $this->logger = LoggingFactory::create(LogLevel::INFO); $this->setDefaultComposerOptions(); } diff --git a/src/Logging/LegacyLogger.php b/src/Logging/LegacyLogger.php new file mode 100644 index 0000000..732671e --- /dev/null +++ b/src/Logging/LegacyLogger.php @@ -0,0 +1,52 @@ + 0, + LogLevel::INFO => 1, + LogLevel::NOTICE => 2, + LogLevel::WARNING => 3, + LogLevel::ERROR => 4, + LogLevel::CRITICAL => 5, + LogLevel::ALERT => 6, + LogLevel::EMERGENCY => 7 + ]; + + /** + * @var int + */ + private $mappedLevel; + + public function __construct(string $level) + { + $this->mappedLevel = self::LEVEL_MAPPING[$level] ?? 1; + } + + /** + * @param mixed $level + * @param string|\Stringable $message + * @param mixed[] $context + * @return void + */ + public function log($level, $message, array $context = array()) + { + if ($this->mapLevelToNumber($level) ?? 1 >= $this->mappedLevel) { + printf("%s (%s)\n", $message, json_encode($context)); + } + } + + private static function mapLevelToNumber(string $level): int + { + return self::LEVEL_MAPPING[$level] ?? 1; + } +} diff --git a/src/Logging/LoggingFactory.php b/src/Logging/LoggingFactory.php new file mode 100644 index 0000000..9a32055 --- /dev/null +++ b/src/Logging/LoggingFactory.php @@ -0,0 +1,19 @@ +mapLevelToNumber($level) ?? 1 >= $this->mappedLevel) { + if ($this->mapLevelToNumber($level) >= $this->mappedLevel) { printf("%s (%s)\n", $message, json_encode($context)); } }