Skip to content

Commit 6fbf601

Browse files
authored
fix: Logger - allow variable {line} to be used without variable {file} when logging message (#9502)
* fix: allow Logger variable {line} to be used without variable {file} * add changelog * phpstan baseline update
1 parent 92d69b4 commit 6fbf601

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

system/Log/Logger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ protected function interpolate($message, array $context = [])
341341
$replace['{env}'] = ENVIRONMENT;
342342

343343
// Allow us to log the file/line that we are logging from
344-
if (str_contains($message, '{file}')) {
344+
if (str_contains($message, '{file}') || str_contains($message, '{line}')) {
345345
[$file, $line] = $this->determineFile();
346346

347347
$replace['{file}'] = $file;

tests/system/Log/LoggerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,23 @@ public function testLogInterpolatesFileAndLine(): void
236236
$this->assertGreaterThan(1, strpos($logs[0], $expected));
237237
}
238238

239+
public function testLogInterpolatesLineOnly(): void
240+
{
241+
$config = new LoggerConfig();
242+
243+
$logger = new Logger($config);
244+
245+
$_ENV['foo'] = 'bar';
246+
247+
$logger->log('debug', 'Test message Sample {line}');
248+
$line = __LINE__ - 1;
249+
$expected = "Sample {$line}";
250+
251+
$logs = TestHandler::getLogs();
252+
253+
$this->assertGreaterThan(1, strpos($logs[0], $expected));
254+
}
255+
239256
public function testLogInterpolatesExceptions(): void
240257
{
241258
$config = new LoggerConfig();

user_guide_src/source/changelogs/v4.6.1.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Bugs Fixed
3434
- **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.
3535
- **Database:** Fixed a bug in ``Postgre`` and ``SQLite3`` handlers where composite unique keys were not fully taken into account for ``upsert`` type of queries.
3636
- **Database:** Fixed a bug in the ``OCI8`` and ``SQLSRV`` drivers where ``getVersion()`` returned an empty string when the database connection was not yet established.
37+
- **Logger:** Fixed a bug where the ``{line}`` variable couldn't be used without specifying the ``{file}`` variable when logging the message.
3738

3839
See the repo's
3940
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_

utils/phpstan-baseline/argument.type.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 147 errors
1+
# total 148 errors
22

33
parameters:
44
ignoreErrors:
@@ -229,7 +229,7 @@ parameters:
229229

230230
-
231231
message: '#^Parameter \#1 \$config of class CodeIgniter\\Log\\Logger constructor expects Config\\Logger, CodeIgniter\\Test\\Mock\\MockLogger given\.$#'
232-
count: 24
232+
count: 25
233233
path: ../../tests/system/Log/LoggerTest.php
234234

235235
-

utils/phpstan-baseline/loader.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 3759 errors
1+
# total 3760 errors
22
includes:
33
- argument.type.neon
44
- assign.propertyType.neon

0 commit comments

Comments
 (0)