Skip to content

Commit 68d7e54

Browse files
authored
feat(log): allow usage of multiple same log channels (tempestphp#718)
1 parent fbaf866 commit 68d7e54

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/Tempest/Log/src/GenericLogger.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
final class GenericLogger implements Logger
1212
{
13-
/** @var array<class-string, Monolog> */
13+
/** @var array<int, Monolog> */
1414
private array $drivers = [];
1515

1616
public function __construct(
@@ -74,14 +74,14 @@ private function writeLog(Level $level, string $message, array $context): void
7474

7575
private function resolveDriver(LogChannel $channel, Level $level): Monolog
7676
{
77-
if (! isset($this->drivers[$channel::class])) {
78-
$this->drivers[$channel::class] = new Monolog(
77+
if (! isset($this->drivers[spl_object_id($channel)])) {
78+
$this->drivers[spl_object_id($channel)] = new Monolog(
7979
name: $this->logConfig->prefix,
8080
handlers: $channel->getHandlers($level),
8181
processors: $channel->getProcessors(),
8282
);
8383
}
8484

85-
return $this->drivers[$channel::class];
85+
return $this->drivers[spl_object_id($channel)];
8686
}
8787
}

src/Tempest/Log/tests/GenericLoggerTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ public function test_append_log_channel_works(): void
3535
$this->assertStringContainsString('test', file_get_contents($filePath));
3636
}
3737

38+
protected function tearDown(): void
39+
{
40+
$files = glob(__DIR__ . '/logs/*.log');
41+
42+
foreach ($files as $file) {
43+
if (is_file($file)) {
44+
unlink($file);
45+
}
46+
}
47+
}
48+
3849
public function test_daily_log_channel_works(): void
3950
{
4051
$filePath = __DIR__ . '/logs/tempest-' . date('Y-m-d') . '.log';
@@ -72,4 +83,26 @@ public function test_weekly_log_channel_works(): void
7283

7384
$this->assertStringContainsString('test', file_get_contents($filePath));
7485
}
86+
87+
public function test_multiple_same_log_channels_works(): void
88+
{
89+
$filePath = __DIR__ . '/logs/multiple-tempest1.log';
90+
$secondFilePath = __DIR__ . '/logs/multiple-tempest2.log';
91+
92+
$config = new LogConfig(
93+
channels: [
94+
new AppendLogChannel($filePath),
95+
new AppendLogChannel($secondFilePath),
96+
],
97+
);
98+
99+
$logger = new GenericLogger($config);
100+
$logger->info('test');
101+
102+
$this->assertFileExists($filePath);
103+
$this->assertStringContainsString('test', file_get_contents($filePath));
104+
105+
$this->assertFileExists($secondFilePath);
106+
$this->assertStringContainsString('test', file_get_contents($secondFilePath));
107+
}
75108
}

0 commit comments

Comments
 (0)