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", diff --git a/composer.json b/composer.json index 3d9354754805..aeac6801da1b 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/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/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/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); } /** 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 } 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 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 diff --git a/user_guide_src/source/general/common_functions.rst b/user_guide_src/source/general/common_functions.rst index a7b701241007..846273551ea5 100755 --- a/user_guide_src/source/general/common_functions.rst +++ b/user_guide_src/source/general/common_functions.rst @@ -324,13 +324,17 @@ 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**, - **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.