-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #555 from php-enqueue/loging-improvements
[consumption] Logging improvements
- Loading branch information
Showing
27 changed files
with
1,214 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Enqueue\Client\ConsumptionExtension; | ||
|
||
use Enqueue\Client\Config; | ||
use Enqueue\Consumption\Context\PostMessageReceived; | ||
use Enqueue\Consumption\Result; | ||
use Enqueue\Util\Stringify; | ||
use Psr\Log\LogLevel; | ||
|
||
class LogExtension extends \Enqueue\Consumption\Extension\LogExtension | ||
{ | ||
public function onPostMessageReceived(PostMessageReceived $context): void | ||
{ | ||
$result = $context->getResult(); | ||
$message = $context->getMessage(); | ||
|
||
$logLevel = Result::REJECT == ((string) $result) ? LogLevel::ERROR : LogLevel::INFO; | ||
|
||
if ($command = $message->getProperty(Config::COMMAND)) { | ||
$reason = ''; | ||
$logMessage = "[client] Processed {command}\t{body}\t{result}"; | ||
if ($result instanceof Result && $result->getReason()) { | ||
$reason = $result->getReason(); | ||
|
||
$logMessage .= ' {reason}'; | ||
} | ||
|
||
$context->getLogger()->log($logLevel, $logMessage, [ | ||
'result' => str_replace('enqueue.', '', $result), | ||
'reason' => $reason, | ||
'command' => $command, | ||
'queueName' => $context->getConsumer()->getQueue()->getQueueName(), | ||
'body' => Stringify::that($message->getBody()), | ||
'properties' => Stringify::that($message->getProperties()), | ||
'headers' => Stringify::that($message->getHeaders()), | ||
]); | ||
|
||
return; | ||
} | ||
|
||
$topic = $message->getProperty(Config::TOPIC); | ||
$processor = $message->getProperty(Config::PROCESSOR); | ||
if ($topic && $processor) { | ||
$reason = ''; | ||
$logMessage = "[client] Processed {topic} -> {processor}\t{body}\t{result}"; | ||
if ($result instanceof Result && $result->getReason()) { | ||
$reason = $result->getReason(); | ||
|
||
$logMessage .= ' {reason}'; | ||
} | ||
|
||
$context->getLogger()->log($logLevel, $logMessage, [ | ||
'result' => str_replace('enqueue.', '', $result), | ||
'reason' => $reason, | ||
'topic' => $topic, | ||
'processor' => $processor, | ||
'queueName' => $context->getConsumer()->getQueue()->getQueueName(), | ||
'body' => Stringify::that($message->getBody()), | ||
'properties' => Stringify::that($message->getProperties()), | ||
'headers' => Stringify::that($message->getHeaders()), | ||
]); | ||
|
||
return; | ||
} | ||
|
||
parent::onPostMessageReceived($context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Enqueue\Consumption\Context; | ||
|
||
use Psr\Log\LoggerInterface; | ||
|
||
class InitLogger | ||
{ | ||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
public function __construct(LoggerInterface $logger) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
|
||
public function getLogger(): LoggerInterface | ||
{ | ||
return $this->logger; | ||
} | ||
|
||
public function changeLogger(LoggerInterface $logger): void | ||
{ | ||
$this->logger = $logger; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Enqueue\Consumption\Extension; | ||
|
||
use Enqueue\Consumption\Context\End; | ||
use Enqueue\Consumption\Context\MessageReceived; | ||
use Enqueue\Consumption\Context\PostMessageReceived; | ||
use Enqueue\Consumption\Context\Start; | ||
use Enqueue\Consumption\EndExtensionInterface; | ||
use Enqueue\Consumption\MessageReceivedExtensionInterface; | ||
use Enqueue\Consumption\PostMessageReceivedExtensionInterface; | ||
use Enqueue\Consumption\Result; | ||
use Enqueue\Consumption\StartExtensionInterface; | ||
use Enqueue\Util\Stringify; | ||
use Psr\Log\LogLevel; | ||
|
||
class LogExtension implements StartExtensionInterface, MessageReceivedExtensionInterface, PostMessageReceivedExtensionInterface, EndExtensionInterface | ||
{ | ||
public function onStart(Start $context): void | ||
{ | ||
$context->getLogger()->debug('Consumption has started'); | ||
} | ||
|
||
public function onEnd(End $context): void | ||
{ | ||
$context->getLogger()->debug('Consumption has ended'); | ||
} | ||
|
||
public function onMessageReceived(MessageReceived $context): void | ||
{ | ||
$message = $context->getMessage(); | ||
|
||
$context->getLogger()->debug("Received from {queueName}\t{body}", [ | ||
'queueName' => $context->getConsumer()->getQueue()->getQueueName(), | ||
'redelivered' => $message->isRedelivered(), | ||
'body' => Stringify::that($message->getBody()), | ||
'properties' => Stringify::that($message->getProperties()), | ||
'headers' => Stringify::that($message->getHeaders()), | ||
]); | ||
} | ||
|
||
public function onPostMessageReceived(PostMessageReceived $context): void | ||
{ | ||
$message = $context->getMessage(); | ||
$queue = $context->getConsumer()->getQueue(); | ||
$result = $context->getResult(); | ||
|
||
$reason = ''; | ||
$logMessage = "Processed from {queueName}\t{body}\t{result}"; | ||
if ($result instanceof Result && $result->getReason()) { | ||
$reason = $result->getReason(); | ||
$logMessage .= ' {reason}'; | ||
} | ||
$logContext = [ | ||
'result' => str_replace('enqueue.', '', $result), | ||
'reason' => $reason, | ||
'queueName' => $queue->getQueueName(), | ||
'body' => Stringify::that($message->getBody()), | ||
'properties' => Stringify::that($message->getProperties()), | ||
'headers' => Stringify::that($message->getHeaders()), | ||
]; | ||
|
||
$logLevel = Result::REJECT == ((string) $result) ? LogLevel::ERROR : LogLevel::INFO; | ||
|
||
$context->getLogger()->log($logLevel, $logMessage, $logContext); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.