1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Werkspot\MessageBus\Bus\DeliveryChain\Middleware; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
use Psr\Log\NullLogger; |
10
|
|
|
use Werkspot\MessageBus\Bus\DeliveryChain\Middleware\Validation\MessageViolationException; |
11
|
|
|
use Werkspot\MessageBus\Bus\DeliveryChain\Middleware\Validation\MessageViolationListException; |
12
|
|
|
use Werkspot\MessageBus\Bus\DeliveryChain\MiddlewareInterface; |
13
|
|
|
use Werkspot\MessageBus\Message\AsynchronousMessage; |
14
|
|
|
use Werkspot\MessageBus\Message\MessageInterface; |
15
|
|
|
|
16
|
|
|
final class LoggingMiddleware implements MiddlewareInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var LoggerInterface |
20
|
|
|
*/ |
21
|
|
|
private $logger; |
22
|
|
|
|
23
|
7 |
|
public function __construct(LoggerInterface $logger = null) |
24
|
|
|
{ |
25
|
7 |
|
$this->logger = $logger ?? new NullLogger(); |
26
|
7 |
|
} |
27
|
|
|
|
28
|
7 |
|
public function deliver(MessageInterface $message, callable $next): void |
29
|
|
|
{ |
30
|
7 |
|
$this->logger->info( |
31
|
7 |
|
sprintf( |
32
|
7 |
|
'%s message "%s"', |
33
|
7 |
|
$message instanceof AsynchronousMessage ? 'Queueing' : 'Executing', |
34
|
7 |
|
$this->getPayloadType($message) |
35
|
|
|
) |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
try { |
39
|
7 |
|
$next($message); |
40
|
4 |
|
} catch (MessageViolationListException $exceptionList) { |
41
|
1 |
|
$this->logValidationException($message, $exceptionList); |
42
|
1 |
|
throw $exceptionList; |
43
|
3 |
|
} catch (Exception $exception) { |
44
|
3 |
|
$this->logException($message, $exception); |
45
|
3 |
|
throw $exception; |
46
|
|
|
} |
47
|
3 |
|
} |
48
|
|
|
|
49
|
1 |
|
private function logValidationException( |
50
|
|
|
MessageInterface $message, |
51
|
|
|
MessageViolationListException $exceptionList |
52
|
|
|
): void { |
53
|
1 |
|
$this->logger->error('Error validating message ' . json_encode($message->getPayload())); |
54
|
|
|
|
55
|
|
|
/** @var MessageViolationException $exception */ |
56
|
1 |
|
foreach ($exceptionList as $key => $exception) { |
57
|
1 |
|
$this->logger->debug(sprintf('Violation error %s: %s', $key, $exception->getMessage())); |
58
|
|
|
} |
59
|
1 |
|
} |
60
|
|
|
|
61
|
3 |
|
private function logException(MessageInterface $message, Exception $exception): void |
62
|
|
|
{ |
63
|
3 |
|
$this->logger->error( |
64
|
3 |
|
sprintf( |
65
|
3 |
|
'%s while handling "%s": %s', |
66
|
3 |
|
get_class($exception), |
67
|
3 |
|
$this->getPayloadType($message), |
68
|
3 |
|
trim($exception->getMessage()) |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
|
72
|
3 |
|
$this->logger->debug($exception->getTraceAsString()); |
73
|
3 |
|
$this->logger->debug(serialize($message)); |
74
|
3 |
|
} |
75
|
|
|
|
76
|
7 |
|
private function getPayloadType(MessageInterface $message): string |
77
|
|
|
{ |
78
|
7 |
|
if (is_object($message->getPayload())) { |
79
|
1 |
|
return get_class($message->getPayload()); |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
if (is_array($message->getPayload())) { |
83
|
1 |
|
return 'array'; |
84
|
|
|
} |
85
|
|
|
|
86
|
5 |
|
return $message->getPayload(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|