Skip to content

Commit bb34d61

Browse files
authored
Merge pull request #7 from apacheborys/introduce-simple-logging
Introduce simple logging
2 parents ea9fcaa + fb8ebd5 commit bb34d61

6 files changed

Lines changed: 153 additions & 67 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
}
1111
],
1212
"require": {
13-
"php": "^7.4||^8.0"
13+
"php": "^7.4||^8.0",
14+
"psr/log": "^1.1"
1415
},
1516
"require-dev": {
1617
"phpunit/phpunit": "9.5.21",

composer.lock

Lines changed: 53 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AbstractHandler.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,26 @@
66
use ApacheBorys\Retry\Entity\Config;
77
use ApacheBorys\Retry\HandlerExceptionDeclarator\StandardHandlerExceptionDeclarator;
88
use ApacheBorys\Retry\Interfaces\HandlerExceptionDeclaratorInterface;
9+
use ApacheBorys\Retry\Traits\LogWrapper;
10+
use Psr\Log\LoggerInterface;
11+
use Psr\Log\LogLevel;
912

1013
abstract class AbstractHandler
1114
{
15+
public const LOG_PREFIX = 'Retry lib: ';
16+
17+
use LogWrapper;
18+
1219
/** @var Config[] */
1320
protected array $config;
1421

22+
protected ?LoggerInterface $logger;
23+
1524
protected HandlerExceptionDeclaratorInterface $declarator;
1625

17-
public function __construct(array $config = [])
26+
public function __construct(array $config = [], LoggerInterface $logger = null)
1827
{
28+
$this->logger = $logger;
1929
$this->config = $this->initConfig($config);
2030
}
2131

@@ -29,8 +39,10 @@ private function initConfig(array $config): array
2939
{
3040
$result = [];
3141

32-
$definerClass = $config['handlerExceptionDeclarator']['class'] ?? StandardHandlerExceptionDeclarator::class;
33-
$this->declarator = new $definerClass(...$config['handlerExceptionDeclarator']['arguments'] ?? []);
42+
$declaratorClass = $config['handlerExceptionDeclarator']['class'] ?? StandardHandlerExceptionDeclarator::class;
43+
$this->declarator = new $declaratorClass(...$config['handlerExceptionDeclarator']['arguments'] ?? []);
44+
45+
$this->sendLogRecordInitDeclarator($declaratorClass);
3446

3547
foreach ($config['items'] as $retryName => $configNode) {
3648
$result[$retryName] = new Config(
@@ -43,6 +55,21 @@ private function initConfig(array $config): array
4355
);
4456
}
4557

58+
$this->sendLogRecordInitConfigs(count($result));
59+
4660
return $result;
4761
}
62+
63+
private function sendLogRecordInitDeclarator(string $declaratorClass): void
64+
{
65+
$this->sendLogRecord(
66+
LogLevel::INFO,
67+
sprintf('Init for %s with handler exception declarator %s', get_class($this), $declaratorClass)
68+
);
69+
}
70+
71+
private function sendLogRecordInitConfigs(int $qty): void
72+
{
73+
$this->sendLogRecord(LogLevel::INFO, sprintf('Init for %s configs. Quantity: %d', get_class($this), $qty));
74+
}
4875
}

src/ExceptionHandler.php

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use ApacheBorys\Retry\Entity\{Config, FormulaItem, Message};
88
use ApacheBorys\Retry\Exceptions\WrongArgument;
99
use ApacheBorys\Retry\ValueObject\{ArgumentType, FormulaArgument};
10+
use Psr\Log\LogLevel;
1011

1112
class ExceptionHandler extends AbstractHandler
1213
{
@@ -34,19 +35,23 @@ private function getHandling($config): callable
3435
if (get_class($exception) === $retryConfig->getHandledException()) {
3536
$tryNumber = $this->getTryNumber($exception, $retryConfig);
3637

38+
$this->sendLogRecordCatchException($retryConfig, $tryNumber);
39+
3740
if ($tryNumber < $retryConfig->getMaxRetries()) {
38-
$retryConfig->getTransport()->send(
39-
new Message(
40-
$retryConfig->getTransport()->getNextId($exception, $retryConfig),
41-
$retryConfig->getName(),
42-
$retryConfig->getExecutor()->getCorrelationId($exception, $retryConfig),
43-
$this->compilePayload($exception, $retryConfig),
44-
$tryNumber,
45-
false,
46-
$this->calculateNextTimeForTry($exception, $retryConfig),
47-
get_class($retryConfig->getExecutor())
48-
)
41+
$message = new Message(
42+
$retryConfig->getTransport()->getNextId($exception, $retryConfig),
43+
$retryConfig->getName(),
44+
$retryConfig->getExecutor()->getCorrelationId($exception, $retryConfig),
45+
$this->compilePayload($exception, $retryConfig),
46+
$tryNumber,
47+
false,
48+
$this->calculateNextTimeForTry($exception, $retryConfig),
49+
get_class($retryConfig->getExecutor())
4950
);
51+
52+
$result = $retryConfig->getTransport()->send($message);
53+
54+
$this->sendLogRecordAboutSentMessage($message, $result);
5055
}
5156

5257
throw $exception;
@@ -105,4 +110,17 @@ private function compileArgument(FormulaItem $item, \Throwable $exception, Confi
105110
);
106111
}
107112
}
113+
114+
private function sendLogRecordCatchException(Config $retryConfig, int $tryNumber): void
115+
{
116+
$this->sendLogRecord(
117+
LogLevel::DEBUG,
118+
sprintf('Catch exception case for %s, try number is %d', $retryConfig->getHandledException(), $tryNumber)
119+
);
120+
}
121+
122+
private function sendLogRecordAboutSentMessage(Message $message, bool $result): void
123+
{
124+
$this->sendLogRecord(LogLevel::DEBUG, sprintf('Send new message id %s. Result is %s', $message->getId(), (string) $result));
125+
}
108126
}

src/MessageHandler.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
namespace ApacheBorys\Retry;
55

6+
use ApacheBorys\Retry\Entity\Config;
7+
use ApacheBorys\Retry\Entity\Message;
68
use ApacheBorys\Retry\Exceptions\{MessageCantMarkAsProcessed, MessageHandlerFailed};
9+
use Psr\Log\LogLevel;
710

811
class MessageHandler extends AbstractHandler
912
{
@@ -17,14 +20,34 @@ public function processRetries(array $processExceptionsOnly = [], int $maxMessag
1720
$messages = $config->getTransport()->fetchUnprocessedMessages($maxMessagesPerException);
1821

1922
foreach ($messages ?? [] as $message) {
23+
$this->sendLogRecordAboutNewMessage($message, $config);
24+
2025
if (!$config->getExecutor()->handle($message)) {
2126
throw new MessageHandlerFailed();
2227
}
2328

2429
if (!$config->getTransport()->markMessageAsProcessed($message)) {
2530
throw new MessageCantMarkAsProcessed();
2631
}
32+
33+
$this->sendLogRecordAboutProcessedMessage($message);
2734
}
2835
}
2936
}
37+
38+
private function sendLogRecordAboutNewMessage(Message $message, Config $config): void
39+
{
40+
$this->sendLogRecord(
41+
LogLevel::DEBUG,
42+
sprintf('Received new message %s for exception %s', $message->getId(), $config->getHandledException())
43+
);
44+
}
45+
46+
private function sendLogRecordAboutProcessedMessage(Message $message): void
47+
{
48+
$this->sendLogRecord(
49+
LogLevel::DEBUG,
50+
sprintf('Received message %s was processed and marked as processed successfully', $message->getId())
51+
);
52+
}
3053
}

src/Traits/LogWrapper.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ApacheBorys\Retry\Traits;
5+
6+
use Psr\Log\LoggerInterface;
7+
8+
trait LogWrapper
9+
{
10+
protected function sendLogRecord(string $level, string $string): void
11+
{
12+
if ($this->logger instanceof LoggerInterface) {
13+
$this->logger->{$level}(self::LOG_PREFIX . $string);
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)