Skip to content

Commit 326a399

Browse files
committed
Fix Logger PSR-3 compliance and interpolation issues
1 parent d9ec94b commit 326a399

2 files changed

Lines changed: 94 additions & 3 deletions

File tree

‎system/Log/Logger.php‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,14 @@ public function log($level, string|Stringable $message, array $context = []): vo
291291
*/
292292
protected function interpolate($message, array $context = [])
293293
{
294-
if (! is_string($message)) {
294+
295+
if (! is_string($message) && ! ($message instanceof Stringable)) {
295296
return print_r($message, true);
296297
}
297298

299+
// Cast to string in case $message is a Stringable object
300+
$message = (string) $message;
301+
298302
$replace = [];
299303

300304
foreach ($context as $key => $val) {
@@ -305,6 +309,10 @@ protected function interpolate($message, array $context = [])
305309
}
306310

307311
// todo - sanitize input before writing to file?
312+
if (is_array($val) || (is_object($val) && ! method_exists($val, '__toString'))) {
313+
$val = print_r($val, true);
314+
}
315+
308316
$replace['{' . $key . '}'] = $val;
309317
}
310318

@@ -322,9 +330,9 @@ protected function interpolate($message, array $context = [])
322330

323331
// Match up environment variables in {env:foo} tags.
324332
if (str_contains($message, 'env:')) {
325-
preg_match('/env:[^}]+/', $message, $matches);
333+
preg_match_all('/env:[^}]+/', $message, $matches);
326334

327-
foreach ($matches as $str) {
335+
foreach ($matches[0] as $str) {
328336
$key = str_replace('env:', '', $str);
329337
$replace["{{$str}}"] = $_ENV[$key] ?? 'n/a';
330338
}

‎tests/system/Log/LoggerTest.php‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use PHPUnit\Framework\Attributes\Group;
2323
use ReflectionMethod;
2424
use ReflectionNamedType;
25+
use stdClass;
26+
use Stringable;
2527
use Tests\Support\Log\Handlers\TestHandler;
2628

2729
/**
@@ -438,4 +440,85 @@ public function testDetermineFileNoStackTrace(): void
438440

439441
$this->assertSame($expected, $logger->determineFile());
440442
}
443+
444+
public function testLogInterpolatesArrayContext(): void
445+
{
446+
$config = new LoggerConfig();
447+
$logger = new Logger($config);
448+
449+
Time::setTestNow('2023-11-25 12:00:00');
450+
451+
$expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message ' . print_r(['foo' => 'bar'], true);
452+
453+
$logger->log('debug', 'Test message {foo}', ['foo' => ['foo' => 'bar']]);
454+
455+
$logs = TestHandler::getLogs();
456+
457+
$this->assertCount(1, $logs);
458+
$this->assertSame($expected, $logs[0]);
459+
}
460+
461+
public function testLogInterpolatesObjectContext(): void
462+
{
463+
$config = new LoggerConfig();
464+
$logger = new Logger($config);
465+
466+
Time::setTestNow('2023-11-25 12:00:00');
467+
468+
$obj = new stdClass();
469+
$obj->foo = 'bar';
470+
471+
$expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message ' . print_r($obj, true);
472+
473+
$logger->log('debug', 'Test message {foo}', ['foo' => $obj]);
474+
475+
$logs = TestHandler::getLogs();
476+
477+
$this->assertCount(1, $logs);
478+
$this->assertSame($expected, $logs[0]);
479+
}
480+
481+
public function testLogInterpolatesMultipleEnvironmentVars(): void
482+
{
483+
$config = new LoggerConfig();
484+
$logger = new Logger($config);
485+
486+
Time::setTestNow('2023-11-25 12:00:00');
487+
488+
$_ENV['foo'] = 'bar';
489+
$_ENV['baz'] = 'qux';
490+
491+
$expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message bar and qux';
492+
493+
$logger->log('debug', 'Test message {env:foo} and {env:baz}');
494+
495+
$logs = TestHandler::getLogs();
496+
497+
$this->assertCount(1, $logs);
498+
$this->assertSame($expected, $logs[0]);
499+
}
500+
501+
public function testLogAcceptsStringableMessage(): void
502+
{
503+
$config = new LoggerConfig();
504+
$logger = new Logger($config);
505+
506+
Time::setTestNow('2023-11-25 12:00:00');
507+
508+
$message = new class () implements Stringable {
509+
public function __toString(): string
510+
{
511+
return 'Stringable message {foo}';
512+
}
513+
};
514+
515+
$expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Stringable message bar';
516+
517+
$logger->log('debug', $message, ['foo' => 'bar']);
518+
519+
$logs = TestHandler::getLogs();
520+
521+
$this->assertCount(1, $logs);
522+
$this->assertSame($expected, $logs[0]);
523+
}
441524
}

0 commit comments

Comments
 (0)