Skip to content

Commit 2461840

Browse files
authored
Merge pull request #2744 from dafriend/Logger-file-line-fix
Logger {file} {line} fix
2 parents 58ee559 + 2a26030 commit 2461840

3 files changed

Lines changed: 39 additions & 25 deletions

File tree

system/Log/Logger.php

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -467,37 +467,52 @@ protected function interpolate($message, array $context = [])
467467
return strtr($message, $replace);
468468
}
469469

470-
//--------------------------------------------------------------------
471470

472471
/**
473-
* Determines the current file/line that the log method was called from.
474-
* by analyzing the backtrace.
472+
* Determines the file and line that the logging call
473+
* was made from by analyzing the backtrace.
474+
* Find the earliest stack frame that is part of our logging system.
475475
*
476476
* @return array
477477
*/
478478
public function determineFile(): array
479479
{
480-
// Determine the file and line by finding the first
481-
// backtrace that is not part of our logging system.
482-
$trace = debug_backtrace();
483-
$file = null;
484-
$line = null;
480+
$logFunctions = [
481+
'log_message',
482+
'log',
483+
'error',
484+
'debug',
485+
'info',
486+
'warning',
487+
'critical',
488+
'emergency',
489+
'alert',
490+
'notice',
491+
];
492+
493+
// Generate Backtrace info
494+
$trace = \debug_backtrace(false);
485495

486-
foreach ($trace as $row)
496+
// So we search from the bottom (earliest) of the stack frames
497+
$stackFrames = \array_reverse($trace);
498+
499+
// Find the first reference to a Logger class method
500+
foreach ($stackFrames as $frame)
487501
{
488-
if (in_array($row['function'], ['interpolate', 'determineFile', 'log', 'log_message']))
502+
if (\in_array($frame['function'], $logFunctions))
489503
{
490-
continue;
504+
$file = isset($frame['file']) ? $this->cleanFileNames($frame['file']) : 'unknown';
505+
$line = $frame['line'] ?? 'unknown';
506+
return [
507+
$file,
508+
$line,
509+
];
491510
}
492-
493-
$file = $row['file'] ?? isset($row['object']) ? get_class($row['object']) : 'unknown';
494-
$line = $row['line'] ?? $row['function'] ?? 'unknown';
495-
break;
496511
}
497512

498513
return [
499-
$file,
500-
$line,
514+
'unknown',
515+
'unknown',
501516
];
502517
}
503518

tests/system/Log/FileHandlerTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ protected function setUp(): void
1717

1818
public function testHandle()
1919
{
20-
$config = new LoggerConfig();
20+
$config = new LoggerConfig();
21+
2122
$config->handlers['Tests\Support\Log\Handlers\TestHandler']['handles'] = ['critical'];
2223

2324
$logger = new MockFileLogger($config->handlers['Tests\Support\Log\Handlers\TestHandler']);
@@ -32,7 +33,8 @@ public function testBasicHandle()
3233
$config = new LoggerConfig();
3334
$config->handlers['Tests\Support\Log\Handlers\TestHandler']['path'] = $this->start . 'charlie/';
3435
$config->handlers['Tests\Support\Log\Handlers\TestHandler']['handles'] = ['critical'];
35-
$logger = new MockFileLogger($config->handlers['Tests\Support\Log\Handlers\TestHandler']);
36+
37+
$logger = new MockFileLogger($config->handlers['Tests\Support\Log\Handlers\TestHandler']);
3638
$logger->setDateFormat('Y-m-d H:i:s:u');
3739
$expected = 'log-' . date('Y-m-d') . '.log';
3840
vfsStream::newFile($expected)->at(vfsStream::setup('root/charlie'))->withContent('This is a test log');

tests/system/Log/LoggerTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,16 +204,13 @@ public function testLogInterpolatesFileAndLine()
204204

205205
$_ENV['foo'] = 'bar';
206206

207-
// For whatever reason, this will often be the class/function instead of file and line.
208-
// Other times it actually returns the line number, so don't look for either
209-
$expected = 'DEBUG - ' . date('Y-m-d') . ' --> Test message LoggerTest';
210-
211207
$logger->log('debug', 'Test message {file} {line}');
208+
$line = __LINE__ - 1;
209+
$expected = "LoggerTest.php $line";
212210

213211
$logs = TestHandler::getLogs();
214212

215-
$this->assertCount(1, $logs);
216-
$this->assertTrue(strpos($logs[0], $expected) === 0);
213+
$this->assertTrue(strpos($logs[0], $expected) > 1);
217214
}
218215

219216
//--------------------------------------------------------------------

0 commit comments

Comments
 (0)