From 94a25b250a94b44485c132612f277390facf19ba Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 24 Sep 2023 17:45:22 +0900 Subject: [PATCH 1/7] chore: update psr/log to v2.0 --- composer.json | 2 +- system/ComposerScripts.php | 3 +- system/ThirdParty/PSR/Log/AbstractLogger.php | 115 +----------------- .../ThirdParty/PSR/Log/LoggerAwareTrait.php | 2 +- system/ThirdParty/PSR/Log/LoggerInterface.php | 36 +++--- system/ThirdParty/PSR/Log/LoggerTrait.php | 36 +++--- system/ThirdParty/PSR/Log/NullLogger.php | 6 +- 7 files changed, 43 insertions(+), 157 deletions(-) diff --git a/composer.json b/composer.json index 646f53d5c784..4affeb06bece 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "ext-json": "*", "ext-mbstring": "*", "laminas/laminas-escaper": "^2.9", - "psr/log": "^1.1" + "psr/log": "^2.0" }, "require-dev": { "codeigniter/coding-standard": "^1.5", diff --git a/system/ComposerScripts.php b/system/ComposerScripts.php index b95bdf54c4ae..021079da468d 100644 --- a/system/ComposerScripts.php +++ b/system/ComposerScripts.php @@ -56,7 +56,7 @@ final class ComposerScripts ], 'psr-log' => [ 'license' => __DIR__ . '/../vendor/psr/log/LICENSE', - 'from' => __DIR__ . '/../vendor/psr/log/Psr/Log/', + 'from' => __DIR__ . '/../vendor/psr/log/src/', 'to' => __DIR__ . '/ThirdParty/PSR/Log/', ], ]; @@ -84,7 +84,6 @@ public static function postUpdate() } self::copyKintInitFiles(); - self::recursiveDelete(self::$dependencies['psr-log']['to'] . 'Test/'); } /** diff --git a/system/ThirdParty/PSR/Log/AbstractLogger.php b/system/ThirdParty/PSR/Log/AbstractLogger.php index e02f9daf3d51..d60a091affae 100644 --- a/system/ThirdParty/PSR/Log/AbstractLogger.php +++ b/system/ThirdParty/PSR/Log/AbstractLogger.php @@ -11,118 +11,5 @@ */ abstract class AbstractLogger implements LoggerInterface { - /** - * System is unusable. - * - * @param string $message - * @param mixed[] $context - * - * @return void - */ - public function emergency($message, array $context = array()) - { - $this->log(LogLevel::EMERGENCY, $message, $context); - } - - /** - * Action must be taken immediately. - * - * Example: Entire website down, database unavailable, etc. This should - * trigger the SMS alerts and wake you up. - * - * @param string $message - * @param mixed[] $context - * - * @return void - */ - public function alert($message, array $context = array()) - { - $this->log(LogLevel::ALERT, $message, $context); - } - - /** - * Critical conditions. - * - * Example: Application component unavailable, unexpected exception. - * - * @param string $message - * @param mixed[] $context - * - * @return void - */ - public function critical($message, array $context = array()) - { - $this->log(LogLevel::CRITICAL, $message, $context); - } - - /** - * Runtime errors that do not require immediate action but should typically - * be logged and monitored. - * - * @param string $message - * @param mixed[] $context - * - * @return void - */ - public function error($message, array $context = array()) - { - $this->log(LogLevel::ERROR, $message, $context); - } - - /** - * Exceptional occurrences that are not errors. - * - * Example: Use of deprecated APIs, poor use of an API, undesirable things - * that are not necessarily wrong. - * - * @param string $message - * @param mixed[] $context - * - * @return void - */ - public function warning($message, array $context = array()) - { - $this->log(LogLevel::WARNING, $message, $context); - } - - /** - * Normal but significant events. - * - * @param string $message - * @param mixed[] $context - * - * @return void - */ - public function notice($message, array $context = array()) - { - $this->log(LogLevel::NOTICE, $message, $context); - } - - /** - * Interesting events. - * - * Example: User logs in, SQL logs. - * - * @param string $message - * @param mixed[] $context - * - * @return void - */ - public function info($message, array $context = array()) - { - $this->log(LogLevel::INFO, $message, $context); - } - - /** - * Detailed debug information. - * - * @param string $message - * @param mixed[] $context - * - * @return void - */ - public function debug($message, array $context = array()) - { - $this->log(LogLevel::DEBUG, $message, $context); - } + use LoggerTrait; } diff --git a/system/ThirdParty/PSR/Log/LoggerAwareTrait.php b/system/ThirdParty/PSR/Log/LoggerAwareTrait.php index 82bf45c89bab..5f1553a4c810 100644 --- a/system/ThirdParty/PSR/Log/LoggerAwareTrait.php +++ b/system/ThirdParty/PSR/Log/LoggerAwareTrait.php @@ -12,7 +12,7 @@ trait LoggerAwareTrait * * @var LoggerInterface|null */ - protected $logger; + protected ?LoggerInterface $logger = null; /** * Sets a logger. diff --git a/system/ThirdParty/PSR/Log/LoggerInterface.php b/system/ThirdParty/PSR/Log/LoggerInterface.php index 2206cfde41ae..b4d062b9b640 100644 --- a/system/ThirdParty/PSR/Log/LoggerInterface.php +++ b/system/ThirdParty/PSR/Log/LoggerInterface.php @@ -22,12 +22,12 @@ interface LoggerInterface /** * System is unusable. * - * @param string $message + * @param string|\Stringable $message * @param mixed[] $context * * @return void */ - public function emergency($message, array $context = array()); + public function emergency(string|\Stringable $message, array $context = []); /** * Action must be taken immediately. @@ -35,35 +35,35 @@ public function emergency($message, array $context = array()); * Example: Entire website down, database unavailable, etc. This should * trigger the SMS alerts and wake you up. * - * @param string $message + * @param string|\Stringable $message * @param mixed[] $context * * @return void */ - public function alert($message, array $context = array()); + public function alert(string|\Stringable $message, array $context = []); /** * Critical conditions. * * Example: Application component unavailable, unexpected exception. * - * @param string $message + * @param string|\Stringable $message * @param mixed[] $context * * @return void */ - public function critical($message, array $context = array()); + public function critical(string|\Stringable $message, array $context = []); /** * Runtime errors that do not require immediate action but should typically * be logged and monitored. * - * @param string $message + * @param string|\Stringable $message * @param mixed[] $context * * @return void */ - public function error($message, array $context = array()); + public function error(string|\Stringable $message, array $context = []); /** * Exceptional occurrences that are not errors. @@ -71,55 +71,55 @@ public function error($message, array $context = array()); * Example: Use of deprecated APIs, poor use of an API, undesirable things * that are not necessarily wrong. * - * @param string $message + * @param string|\Stringable $message * @param mixed[] $context * * @return void */ - public function warning($message, array $context = array()); + public function warning(string|\Stringable $message, array $context = []); /** * Normal but significant events. * - * @param string $message + * @param string|\Stringable $message * @param mixed[] $context * * @return void */ - public function notice($message, array $context = array()); + public function notice(string|\Stringable $message, array $context = []); /** * Interesting events. * * Example: User logs in, SQL logs. * - * @param string $message + * @param string|\Stringable $message * @param mixed[] $context * * @return void */ - public function info($message, array $context = array()); + public function info(string|\Stringable $message, array $context = []); /** * Detailed debug information. * - * @param string $message + * @param string|\Stringable $message * @param mixed[] $context * * @return void */ - public function debug($message, array $context = array()); + public function debug(string|\Stringable $message, array $context = []); /** * Logs with an arbitrary level. * * @param mixed $level - * @param string $message + * @param string|\Stringable $message * @param mixed[] $context * * @return void * * @throws \Psr\Log\InvalidArgumentException */ - public function log($level, $message, array $context = array()); + public function log($level, string|\Stringable $message, array $context = []); } diff --git a/system/ThirdParty/PSR/Log/LoggerTrait.php b/system/ThirdParty/PSR/Log/LoggerTrait.php index e392fef0a0cb..920bda77f835 100644 --- a/system/ThirdParty/PSR/Log/LoggerTrait.php +++ b/system/ThirdParty/PSR/Log/LoggerTrait.php @@ -15,12 +15,12 @@ trait LoggerTrait /** * System is unusable. * - * @param string $message + * @param string|\Stringable $message * @param array $context * * @return void */ - public function emergency($message, array $context = array()) + public function emergency(string|\Stringable $message, array $context = []) { $this->log(LogLevel::EMERGENCY, $message, $context); } @@ -31,12 +31,12 @@ public function emergency($message, array $context = array()) * Example: Entire website down, database unavailable, etc. This should * trigger the SMS alerts and wake you up. * - * @param string $message + * @param string|\Stringable $message * @param array $context * * @return void */ - public function alert($message, array $context = array()) + public function alert(string|\Stringable $message, array $context = []) { $this->log(LogLevel::ALERT, $message, $context); } @@ -46,12 +46,12 @@ public function alert($message, array $context = array()) * * Example: Application component unavailable, unexpected exception. * - * @param string $message + * @param string|\Stringable $message * @param array $context * * @return void */ - public function critical($message, array $context = array()) + public function critical(string|\Stringable $message, array $context = []) { $this->log(LogLevel::CRITICAL, $message, $context); } @@ -60,12 +60,12 @@ public function critical($message, array $context = array()) * Runtime errors that do not require immediate action but should typically * be logged and monitored. * - * @param string $message + * @param string|\Stringable $message * @param array $context * * @return void */ - public function error($message, array $context = array()) + public function error(string|\Stringable $message, array $context = []) { $this->log(LogLevel::ERROR, $message, $context); } @@ -76,12 +76,12 @@ public function error($message, array $context = array()) * Example: Use of deprecated APIs, poor use of an API, undesirable things * that are not necessarily wrong. * - * @param string $message + * @param string|\Stringable $message * @param array $context * * @return void */ - public function warning($message, array $context = array()) + public function warning(string|\Stringable $message, array $context = []) { $this->log(LogLevel::WARNING, $message, $context); } @@ -89,12 +89,12 @@ public function warning($message, array $context = array()) /** * Normal but significant events. * - * @param string $message + * @param string|\Stringable $message * @param array $context * * @return void */ - public function notice($message, array $context = array()) + public function notice(string|\Stringable $message, array $context = []) { $this->log(LogLevel::NOTICE, $message, $context); } @@ -104,12 +104,12 @@ public function notice($message, array $context = array()) * * Example: User logs in, SQL logs. * - * @param string $message + * @param string|\Stringable $message * @param array $context * * @return void */ - public function info($message, array $context = array()) + public function info(string|\Stringable $message, array $context = []) { $this->log(LogLevel::INFO, $message, $context); } @@ -117,12 +117,12 @@ public function info($message, array $context = array()) /** * Detailed debug information. * - * @param string $message + * @param string|\Stringable $message * @param array $context * * @return void */ - public function debug($message, array $context = array()) + public function debug(string|\Stringable $message, array $context = []) { $this->log(LogLevel::DEBUG, $message, $context); } @@ -131,12 +131,12 @@ public function debug($message, array $context = array()) * Logs with an arbitrary level. * * @param mixed $level - * @param string $message + * @param string|\Stringable $message * @param array $context * * @return void * * @throws \Psr\Log\InvalidArgumentException */ - abstract public function log($level, $message, array $context = array()); + abstract public function log($level, string|\Stringable $message, array $context = []); } diff --git a/system/ThirdParty/PSR/Log/NullLogger.php b/system/ThirdParty/PSR/Log/NullLogger.php index c8f7293b1c66..56077057159d 100644 --- a/system/ThirdParty/PSR/Log/NullLogger.php +++ b/system/ThirdParty/PSR/Log/NullLogger.php @@ -16,14 +16,14 @@ class NullLogger extends AbstractLogger * Logs with an arbitrary level. * * @param mixed $level - * @param string $message - * @param array $context + * @param string|\Stringable $message + * @param array $context * * @return void * * @throws \Psr\Log\InvalidArgumentException */ - public function log($level, $message, array $context = array()) + public function log($level, string|\Stringable $message, array $context = []) { // noop } From ec2af9411bf816a1d40d0ad50b0307b37da528f1 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 24 Sep 2023 17:46:25 +0900 Subject: [PATCH 2/7] fix: incorrect Logger interface --- system/Common.php | 8 +++++--- system/Log/Logger.php | 39 +++++++++++++++++++------------------- system/Test/TestLogger.php | 5 +++-- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/system/Common.php b/system/Common.php index ebfd8e2565e8..4ec6cbbbadbf 100644 --- a/system/Common.php +++ b/system/Common.php @@ -794,7 +794,7 @@ function lang(string $line, array $args = [], ?string $locale = null) * - info * - debug * - * @return bool + * @return void */ function log_message(string $level, string $message, array $context = []) { @@ -804,10 +804,12 @@ function log_message(string $level, string $message, array $context = []) if (ENVIRONMENT === 'testing') { $logger = new TestLogger(new Logger()); - return $logger->log($level, $message, $context); + $logger->log($level, $message, $context); + + return; } - return Services::logger(true)->log($level, $message, $context); // @codeCoverageIgnore + Services::logger(true)->log($level, $message, $context); // @codeCoverageIgnore } } diff --git a/system/Log/Logger.php b/system/Log/Logger.php index b1583f5d789c..4d4e36a8afad 100644 --- a/system/Log/Logger.php +++ b/system/Log/Logger.php @@ -15,6 +15,7 @@ use CodeIgniter\Log\Handlers\HandlerInterface; use Psr\Log\LoggerInterface; use RuntimeException; +use Stringable; use Throwable; /** @@ -157,9 +158,9 @@ public function __construct($config, bool $debug = CI_DEBUG) * * @param string $message */ - public function emergency($message, array $context = []): bool + public function emergency(string|Stringable $message, array $context = []): void { - return $this->log('emergency', $message, $context); + $this->log('emergency', $message, $context); } /** @@ -170,9 +171,9 @@ public function emergency($message, array $context = []): bool * * @param string $message */ - public function alert($message, array $context = []): bool + public function alert(string|Stringable $message, array $context = []): void { - return $this->log('alert', $message, $context); + $this->log('alert', $message, $context); } /** @@ -182,9 +183,9 @@ public function alert($message, array $context = []): bool * * @param string $message */ - public function critical($message, array $context = []): bool + public function critical(string|Stringable $message, array $context = []): void { - return $this->log('critical', $message, $context); + $this->log('critical', $message, $context); } /** @@ -193,9 +194,9 @@ public function critical($message, array $context = []): bool * * @param string $message */ - public function error($message, array $context = []): bool + public function error(string|Stringable $message, array $context = []): void { - return $this->log('error', $message, $context); + $this->log('error', $message, $context); } /** @@ -206,9 +207,9 @@ public function error($message, array $context = []): bool * * @param string $message */ - public function warning($message, array $context = []): bool + public function warning(string|Stringable $message, array $context = []): void { - return $this->log('warning', $message, $context); + $this->log('warning', $message, $context); } /** @@ -216,9 +217,9 @@ public function warning($message, array $context = []): bool * * @param string $message */ - public function notice($message, array $context = []): bool + public function notice(string|Stringable $message, array $context = []): void { - return $this->log('notice', $message, $context); + $this->log('notice', $message, $context); } /** @@ -228,9 +229,9 @@ public function notice($message, array $context = []): bool * * @param string $message */ - public function info($message, array $context = []): bool + public function info(string|Stringable $message, array $context = []): void { - return $this->log('info', $message, $context); + $this->log('info', $message, $context); } /** @@ -238,9 +239,9 @@ public function info($message, array $context = []): bool * * @param string $message */ - public function debug($message, array $context = []): bool + public function debug(string|Stringable $message, array $context = []): void { - return $this->log('debug', $message, $context); + $this->log('debug', $message, $context); } /** @@ -249,7 +250,7 @@ public function debug($message, array $context = []): bool * @param string $level * @param string $message */ - public function log($level, $message, array $context = []): bool + public function log($level, string|Stringable $message, array $context = []): void { if (is_numeric($level)) { $level = array_search((int) $level, $this->logLevels, true); @@ -262,7 +263,7 @@ public function log($level, $message, array $context = []): bool // Does the app want to log this right now? if (! in_array($level, $this->loggableLevels, true)) { - return false; + return; } // Parse our placeholders @@ -295,8 +296,6 @@ public function log($level, $message, array $context = []): bool break; } } - - return true; } /** diff --git a/system/Test/TestLogger.php b/system/Test/TestLogger.php index 240f5c6d95b5..dce1277d98d2 100644 --- a/system/Test/TestLogger.php +++ b/system/Test/TestLogger.php @@ -12,6 +12,7 @@ namespace CodeIgniter\Test; use CodeIgniter\Log\Logger; +use Stringable; /** * @see \CodeIgniter\Test\TestLoggerTest @@ -27,7 +28,7 @@ class TestLogger extends Logger * @param string $level * @param string $message */ - public function log($level, $message, array $context = []): bool + public function log($level, string|Stringable $message, array $context = []): void { // While this requires duplicate work, we want to ensure // we have the final message to test against. @@ -52,7 +53,7 @@ public function log($level, $message, array $context = []): bool ]; // Let the parent do it's thing. - return parent::log($level, $message, $context); + parent::log($level, $message, $context); } /** From 079d80103dfee8e751a2cee05d0c4be188ad335b Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 24 Sep 2023 17:50:23 +0900 Subject: [PATCH 3/7] chore: update psr/log to ^2.0 --- admin/framework/composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/framework/composer.json b/admin/framework/composer.json index 70b51ba8f88c..132932acc25d 100644 --- a/admin/framework/composer.json +++ b/admin/framework/composer.json @@ -15,7 +15,7 @@ "ext-json": "*", "ext-mbstring": "*", "laminas/laminas-escaper": "^2.9", - "psr/log": "^1.1" + "psr/log": "^2.0" }, "require-dev": { "codeigniter/coding-standard": "^1.5", From 88cc5722a8e010e3943389d2b02c4c52d6ef25c8 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 24 Sep 2023 18:01:37 +0900 Subject: [PATCH 4/7] test: update test code --- tests/system/Log/LoggerTest.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/tests/system/Log/LoggerTest.php b/tests/system/Log/LoggerTest.php index 317ad9c14bf4..8087ff645845 100644 --- a/tests/system/Log/LoggerTest.php +++ b/tests/system/Log/LoggerTest.php @@ -17,6 +17,7 @@ use CodeIgniter\Test\Mock\MockLogger as LoggerConfig; use Exception; use Tests\Support\Log\Handlers\TestHandler; +use TypeError; /** * @internal @@ -48,14 +49,14 @@ public function testLogThrowsExceptionOnInvalidLevel(): void $logger->log('foo', ''); } - public function testLogReturnsFalseWhenLogNotHandled(): void + public function testLogAlwaysReturnsVoid(): void { $config = new LoggerConfig(); $config->threshold = 3; $logger = new Logger($config); - $this->assertFalse($logger->log('debug', '')); + $this->assertNull($logger->log('debug', '')); } public function testLogActuallyLogs(): void @@ -361,16 +362,12 @@ public function testLogLevels(): void public function testNonStringMessage(): void { + $this->expectException(TypeError::class); + $config = new LoggerConfig(); $logger = new Logger($config); - $expected = '[Tests\Support\Log\Handlers\TestHandler]'; $logger->log(5, $config); - - $logs = TestHandler::getLogs(); - - $this->assertCount(1, $logs); - $this->assertStringContainsString($expected, $logs[0]); } public function testDetermineFileNoStackTrace(): void From d64615bba80629519503a6cb18dbdbace225ab2a Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 24 Sep 2023 18:10:29 +0900 Subject: [PATCH 5/7] docs: update return type of log_message() --- user_guide_src/source/general/common_functions.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/user_guide_src/source/general/common_functions.rst b/user_guide_src/source/general/common_functions.rst index a7b701241007..af6ab261cb59 100755 --- a/user_guide_src/source/general/common_functions.rst +++ b/user_guide_src/source/general/common_functions.rst @@ -324,9 +324,13 @@ Miscellaneous Functions :param string $level: The level of severity :param string $message: The message that is to be logged. :param array $context: An associative array of tags and their values that should be replaced in $message - :returns: true if was logged successfully or false if there was a problem logging it + :returns: void :rtype: bool + .. note:: Since v4.5.0, the return value is fixed to be compatible with PSR + Log. In previous versions, it returned ``true`` if was logged successfully + or ``false`` if there was a problem logging it. + Logs a message using the Log Handlers defined in **app/Config/Logger.php**. Level can be one of the following values: **emergency**, **alert**, **critical**, **error**, **warning**, From 7d31b07ec546cdafea0b2c28de3795e1f5e1ccac Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 24 Sep 2023 18:13:31 +0900 Subject: [PATCH 6/7] docs: change text decoration --- user_guide_src/source/general/common_functions.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/user_guide_src/source/general/common_functions.rst b/user_guide_src/source/general/common_functions.rst index af6ab261cb59..846273551ea5 100755 --- a/user_guide_src/source/general/common_functions.rst +++ b/user_guide_src/source/general/common_functions.rst @@ -333,8 +333,8 @@ Miscellaneous Functions Logs a message using the Log Handlers defined in **app/Config/Logger.php**. - Level can be one of the following values: **emergency**, **alert**, **critical**, **error**, **warning**, - **notice**, **info**, or **debug**. + Level can be one of the following values: ``emergency``, ``alert``, ``critical``, ``error``, ``warning``, + ``notice``, ``info``, or ``debug``. Context can be used to substitute values in the message string. For full details, see the :doc:`Logging Information ` page. From 7dd0ffd5d9335bbe3fece1fd46384faef9a9e5ce Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 25 Sep 2023 09:49:42 +0900 Subject: [PATCH 7/7] docs: add changelog --- user_guide_src/source/changelogs/v4.5.0.rst | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/user_guide_src/source/changelogs/v4.5.0.rst b/user_guide_src/source/changelogs/v4.5.0.rst index 50dae35cd5a2..8bff44c26ab0 100644 --- a/user_guide_src/source/changelogs/v4.5.0.rst +++ b/user_guide_src/source/changelogs/v4.5.0.rst @@ -21,12 +21,28 @@ BREAKING Behavior Changes ================ +- **Logger:** The :php:func:`log_message()` function and the logger methods in + ``CodeIgniter\Log\Logger`` now do not return ``bool`` values. The return types + have been fixed to ``void`` to follow the PSR-3 interface. + Interface Changes ================= +.. note:: As long as you have not extended the relevant CodeIgniter core classes + or implemented these interfaces, all these changes are backward compatible + and require no intervention. + +- **Logger:** The `psr/log `_ package has + been upgraded to v2.0.0. + Method Signature Changes ======================== +- **Logger:** The method signatures of the methods in ``CodeIgniter\Log\Logger`` + that implements the PSR-3 interface have been fixed. The ``bool`` return + types are changed to ``void``. The ``$message`` parameters now have + ``string|Stringable`` types. + .. _v450-removed-deprecated-items: Removed Deprecated Items