Skip to content

Commit

Permalink
minor #27066 [Messenger] unwrap ReceivedMessage in LoggingMiddleware …
Browse files Browse the repository at this point in the history
…to improve log detail (kbond)

This PR was merged into the 4.1-dev branch.

Discussion
----------

[Messenger] unwrap ReceivedMessage in LoggingMiddleware to improve log detail

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

The change gives better visibility in your logs when consuming messages.

Commits
-------

78bb025 [Messenger] unwrap ReceivedMessage in LoggingMiddleware to improve log detail
  • Loading branch information
Tobion committed Apr 26, 2018
2 parents ee0967f + 78bb025 commit 3b363a8
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/Symfony/Component/Messenger/Middleware/LoggingMiddleware.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Messenger\Middleware;

use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage;
use Symfony\Component\Messenger\MiddlewareInterface;
use Psr\Log\LoggerInterface;

Expand All @@ -31,28 +32,33 @@ public function __construct(LoggerInterface $logger)
*/
public function handle($message, callable $next)
{
$this->logger->debug('Starting handling message {class}', array(
'message' => $message,
'class' => \get_class($message),
));
$this->logger->debug('Starting handling message {class}', $this->createContext($message));

try {
$result = $next($message);
} catch (\Throwable $e) {
$this->logger->warning('An exception occurred while handling message {class}', array(
'message' => $message,
'exception' => $e,
'class' => \get_class($message),
$this->logger->warning('An exception occurred while handling message {class}', array_merge(
$this->createContext($message),
array('exception' => $e)
));

throw $e;
}

$this->logger->debug('Finished handling message {class}', array(
'message' => $message,
'class' => \get_class($message),
));
$this->logger->debug('Finished handling message {class}', $this->createContext($message));

return $result;
}

private function createContext($message): array
{
if ($message instanceof ReceivedMessage) {
$message = $message->getMessage();
}

return array(
'message' => $message,
'class' => \get_class($message),
);
}
}

0 comments on commit 3b363a8

Please sign in to comment.