Skip to content

fix: Logger - allow variable {line} to be used without variable {file} when logging message #9502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion system/Log/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions tests/system/Log/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No?

Suggested change
$this->assertGreaterThan(1, strpos($logs[0], $expected));
$this->assertTrue(str_contains($logs[0], $expected));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can also write it this way, but I wanted to use the same style as in the previous test. This will ensure that the correct text is added to the log message. The logged message should not start with the $expected string.

}

public function testLogInterpolatesExceptions(): void
{
$config = new LoggerConfig();
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.6.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
Expand Down
4 changes: 2 additions & 2 deletions utils/phpstan-baseline/argument.type.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 147 errors
# total 148 errors

parameters:
ignoreErrors:
Expand Down Expand Up @@ -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

-
Expand Down
2 changes: 1 addition & 1 deletion utils/phpstan-baseline/loader.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 3759 errors
# total 3760 errors
includes:
- argument.type.neon
- assign.propertyType.neon
Expand Down
Loading