From ef41d7cb68bc306cd71aecb0b88450ae7ace3ac7 Mon Sep 17 00:00:00 2001 From: michalsn Date: Thu, 3 Apr 2025 08:08:11 +0200 Subject: [PATCH 1/3] fix: allow Logger variable {line} to be used without variable {file} --- system/Log/Logger.php | 2 +- tests/system/Log/LoggerTest.php | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/system/Log/Logger.php b/system/Log/Logger.php index 6762e3380ecf..7c14fcdaeb5b 100644 --- a/system/Log/Logger.php +++ b/system/Log/Logger.php @@ -341,7 +341,7 @@ protected function interpolate($message, array $context = []) $replace['{env}'] = ENVIRONMENT; // Allow us to log the file/line that we are logging from - if (str_contains($message, '{file}')) { + if (str_contains($message, '{file}') || str_contains($message, '{line}')) { [$file, $line] = $this->determineFile(); $replace['{file}'] = $file; diff --git a/tests/system/Log/LoggerTest.php b/tests/system/Log/LoggerTest.php index 569de4dbd33d..695e99fa3ef1 100644 --- a/tests/system/Log/LoggerTest.php +++ b/tests/system/Log/LoggerTest.php @@ -236,6 +236,23 @@ public function testLogInterpolatesFileAndLine(): void $this->assertGreaterThan(1, strpos($logs[0], $expected)); } + public function testLogInterpolatesLineOnly(): void + { + $config = new LoggerConfig(); + + $logger = new Logger($config); + + $_ENV['foo'] = 'bar'; + + $logger->log('debug', 'Test message Sample {line}'); + $line = __LINE__ - 1; + $expected = "Sample {$line}"; + + $logs = TestHandler::getLogs(); + + $this->assertGreaterThan(1, strpos($logs[0], $expected)); + } + public function testLogInterpolatesExceptions(): void { $config = new LoggerConfig(); From 2f3d348969ee94880be961c7608db8d0f70a3207 Mon Sep 17 00:00:00 2001 From: michalsn Date: Thu, 3 Apr 2025 08:12:29 +0200 Subject: [PATCH 2/3] add changelog --- user_guide_src/source/changelogs/v4.6.1.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/user_guide_src/source/changelogs/v4.6.1.rst b/user_guide_src/source/changelogs/v4.6.1.rst index 7b35ad9faabc..77fa4799e37e 100644 --- a/user_guide_src/source/changelogs/v4.6.1.rst +++ b/user_guide_src/source/changelogs/v4.6.1.rst @@ -34,6 +34,7 @@ Bugs Fixed - **Cors:** Fixed a bug in the Cors filter that caused the appropriate headers to not be added when another filter returned a response object in the ``before`` filter. - **Database:** Fixed a bug in ``Postgre`` and ``SQLite3`` handlers where composite unique keys were not fully taken into account for ``upsert`` type of queries. - **Database:** Fixed a bug in the ``OCI8`` and ``SQLSRV`` drivers where ``getVersion()`` returned an empty string when the database connection was not yet established. +- **Logger:** Fixed a bug where the ``{line}`` variable couldn't be used without specifying the ``{file}`` variable when logging the message. See the repo's `CHANGELOG.md `_ From e8b15530c8af687595dca31125e3f2fb70abf040 Mon Sep 17 00:00:00 2001 From: michalsn Date: Thu, 3 Apr 2025 08:25:53 +0200 Subject: [PATCH 3/3] phpstan baseline update --- utils/phpstan-baseline/argument.type.neon | 4 ++-- utils/phpstan-baseline/loader.neon | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/utils/phpstan-baseline/argument.type.neon b/utils/phpstan-baseline/argument.type.neon index 2f2e9f3baa38..011377a40084 100644 --- a/utils/phpstan-baseline/argument.type.neon +++ b/utils/phpstan-baseline/argument.type.neon @@ -1,4 +1,4 @@ -# total 147 errors +# total 148 errors parameters: ignoreErrors: @@ -229,7 +229,7 @@ parameters: - message: '#^Parameter \#1 \$config of class CodeIgniter\\Log\\Logger constructor expects Config\\Logger, CodeIgniter\\Test\\Mock\\MockLogger given\.$#' - count: 24 + count: 25 path: ../../tests/system/Log/LoggerTest.php - diff --git a/utils/phpstan-baseline/loader.neon b/utils/phpstan-baseline/loader.neon index f795aabb7fb5..11c2852f5c57 100644 --- a/utils/phpstan-baseline/loader.neon +++ b/utils/phpstan-baseline/loader.neon @@ -1,4 +1,4 @@ -# total 3759 errors +# total 3760 errors includes: - argument.type.neon - assign.propertyType.neon