Skip to content

Commit 95d576a

Browse files
committed
refactor(logging): improve AsyncLogProcessor with enhanced encapsulation and exception handling
- Encapsulated Fiber logic in a dedicated `startFiber` method to improve code clarity. - Implemented `processRemaining` method to ensure all queued logs are processed before object destruction. - Ensured `processRemaining` is called in the destructor to avoid losing any log records.
1 parent 29fa8f0 commit 95d576a

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

src/Decorator/AsyncLogger.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public function __construct(Logger $logger, int $batchSize = 10)
1717
{
1818
parent::__construct($logger);
1919
$this->processor = new AsyncLogProcessor($logger, $batchSize);
20+
21+
// Register shutdown function to ensure logs are processed
22+
register_shutdown_function([$this, 'shutdown']);
23+
2024
}
2125

2226
public function log(LogLevel $level, \Stringable|string $message, array $context = []): void
@@ -25,9 +29,13 @@ public function log(LogLevel $level, \Stringable|string $message, array $context
2529
$this->processor->enqueue($record);
2630
}
2731

32+
public function shutdown(): void
33+
{
34+
$this->processor->processRemaining();
35+
}
36+
2837
public function __destruct()
2938
{
30-
// Explicitly call the destructor to ensure all logs are processed before object is destroyed
31-
$this->processor->__destruct();
39+
$this->shutdown();
3240
}
3341
}

src/Processor/AsyncLogProcessor.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,42 @@ public function enqueue(LogRecord $record): void
2525
private function ensureProcessingStarted(): void
2626
{
2727
if (null === $this->processingFiber || $this->processingFiber->isTerminated()) {
28-
$this->processingFiber = new \Fiber(function (): void {
29-
while (!empty($this->queue)) {
30-
$batch = array_splice($this->queue, 0, $this->batchSize);
31-
foreach ($batch as $record) {
32-
$this->logger->log($record->level, $record->message, $record->context);
33-
\Fiber::suspend(); // Cooperatively yield control
34-
}
35-
}
36-
});
37-
$this->processingFiber->start();
28+
$this->startFiber();
3829
} elseif ($this->processingFiber->isSuspended()) {
3930
$this->processingFiber->resume();
4031
}
4132
}
4233

43-
public function __destruct()
34+
private function startFiber(): void
35+
{
36+
$this->processingFiber = new \Fiber(function (): void {
37+
while (!empty($this->queue)) {
38+
$batch = array_splice($this->queue, 0, $this->batchSize);
39+
foreach ($batch as $record) {
40+
$this->processRecord($record);
41+
\Fiber::suspend(); // Cooperatively yield control
42+
}
43+
}
44+
});
45+
46+
$this->processingFiber->start();
47+
}
48+
49+
private function processRecord(LogRecord $record): void
50+
{
51+
$this->logger->log($record->level, $record->message, $record->context);
52+
}
53+
54+
public function processRemaining(): void
4455
{
4556
while (!empty($this->queue)) {
4657
$this->ensureProcessingStarted();
4758
}
4859
}
60+
61+
62+
public function __destruct()
63+
{
64+
$this->processRemaining();
65+
}
4966
}

0 commit comments

Comments
 (0)