Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Messenger] deprecate LoggingMiddleware in favor of providing a logger to SendMessageMiddleware #30539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 UPGRADE-4.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Messenger
---------

* `Amqp` transport does not throw `\AMQPException` anymore, catch `TransportException` instead.
* Deprecated the `LoggingMiddleware` class, pass a logger to `SendMessageMiddleware` instead.

Routing
-------
Expand Down
5 changes: 5 additions & 0 deletions 5 UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ HttpKernel
* Removed `ConfigDataCollector::getApplicationName()`
* Removed `ConfigDataCollector::getApplicationVersion()`

Messenger
---------

* The `LoggingMiddleware` class has been removed, pass a logger to `SendMessageMiddleware` instead.

Monolog
-------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1598,7 +1598,7 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
}

$defaultMiddleware = [
'before' => [['id' => 'logging']],
'before' => [],
'after' => [['id' => 'send_message'], ['id' => 'handle_message']],
];
foreach ($config['buses'] as $busId => $bus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
<argument type="collection" /> <!-- Messages to send and handle -->
</service>
<service id="messenger.middleware.send_message" class="Symfony\Component\Messenger\Middleware\SendMessageMiddleware">
<tag name="monolog.logger" channel="messenger" />
<argument type="service" id="messenger.senders_locator" />
<call method="setLogger">
<argument type="service" id="logger" on-invalid="ignore" />
</call>
</service>

<!-- Message encoding/decoding -->
Expand All @@ -28,7 +32,11 @@

<!-- Middleware -->
<service id="messenger.middleware.handle_message" class="Symfony\Component\Messenger\Middleware\HandleMessageMiddleware" abstract="true">
<tag name="monolog.logger" channel="messenger" />
<argument /> <!-- Bus handler resolver -->
<call method="setLogger">
<argument type="service" id="logger" on-invalid="ignore" />
</call>
</service>

<service id="messenger.middleware.validation" class="Symfony\Component\Messenger\Middleware\ValidationMiddleware">
Expand All @@ -39,12 +47,6 @@
<argument type="service" id="debug.stopwatch" />
</service>

<!-- Logging -->
<service id="messenger.middleware.logging" class="Symfony\Component\Messenger\Middleware\LoggingMiddleware">
<tag name="monolog.logger" channel="messenger" />
<argument type="service" id="logger" />
</service>

<!-- Discovery -->
<service id="messenger.receiver_locator">
<tag name="container.service_locator" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -701,14 +701,12 @@ public function testMessengerWithMultipleBuses()
$this->assertTrue($container->has('messenger.bus.commands'));
$this->assertSame([], $container->getDefinition('messenger.bus.commands')->getArgument(0));
$this->assertEquals([
['id' => 'logging'],
['id' => 'send_message'],
['id' => 'handle_message'],
], $container->getParameter('messenger.bus.commands.middleware'));
$this->assertTrue($container->has('messenger.bus.events'));
$this->assertSame([], $container->getDefinition('messenger.bus.events')->getArgument(0));
$this->assertEquals([
['id' => 'logging'],
['id' => 'with_factory', 'arguments' => ['foo', true, ['bar' => 'baz']]],
['id' => 'send_message'],
['id' => 'handle_message'],
Expand Down
4 changes: 1 addition & 3 deletions 4 src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ CHANGELOG

* Added `PhpSerializer` which uses PHP's native `serialize()` and
`unserialize()` to serialize messages to a transport

* [BC BREAK] If no serializer were passed, the default serializer
changed from `Serializer` to `PhpSerializer` inside `AmqpReceiver`,
`AmqpSender`, `AmqpTransport` and `AmqpTransportFactory`.

* Added `TransportException` to mark an exception transport-related

* [BC BREAK] If listening to exceptions while using `AmqpSender` or `AmqpReceiver`, `\AMQPException` is
no longer thrown in favor of `TransportException`.
* Deprecated `LoggingMiddleware`, pass a logger to `SendMessageMiddleware` instead.

4.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ protected function execute(InputInterface $input, OutputInterface $output): void

$io->comment('Quit the worker with CONTROL-C.');

if (!$output->isDebug()) {
$io->comment('Re-run the command with a -vvv option to see logs about consumed messages.');
if (OutputInterface::VERBOSITY_VERBOSE > $output->getVerbosity()) {
$io->comment('Re-run the command with a -vv option to see logs about consumed messages.');
}

$worker = new Worker($receiver, $bus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Messenger\Middleware;

use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\NoHandlerForMessageException;
use Symfony\Component\Messenger\Handler\HandlersLocatorInterface;
Expand All @@ -23,13 +25,16 @@
*/
class HandleMessageMiddleware implements MiddlewareInterface
{
use LoggerAwareTrait;

private $handlersLocator;
private $allowNoHandlers;

public function __construct(HandlersLocatorInterface $handlersLocator, bool $allowNoHandlers = false)
{
$this->handlersLocator = $handlersLocator;
$this->allowNoHandlers = $allowNoHandlers;
$this->logger = new NullLogger();
}

/**
Expand All @@ -41,11 +46,24 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$handler = null;
$message = $envelope->getMessage();

$context = [
'message' => $message,
'class' => \get_class($message),
];

foreach ($this->handlersLocator->getHandlers($envelope) as $alias => $handler) {
$envelope = $envelope->with(HandledStamp::fromCallable($handler, $handler($message), \is_string($alias) ? $alias : null));
$handledStamp = HandledStamp::fromCallable($handler, $handler($message), \is_string($alias) ? $alias : null);
$envelope = $envelope->with($handledStamp);
$this->logger->info('Message "{class}" handled by "{handler}"', $context + ['handler' => $handledStamp->getCallableName()]);
}
if (null === $handler && !$this->allowNoHandlers) {
throw new NoHandlerForMessageException(sprintf('No handler for message "%s".', \get_class($envelope->getMessage())));

if (null === $handler) {
if (!$this->allowNoHandlers) {
throw new NoHandlerForMessageException(sprintf('No handler for message "%s".', $context['class']));
}

$this->logger->info('No handler for message "{class}"', $context);
}

return $stack->next()->handle($envelope, $stack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

namespace Symfony\Component\Messenger\Middleware;

@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, pass a logger to SendMessageMiddleware instead.', LoggingMiddleware::class), E_USER_DEPRECATED);

use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Envelope;

/**
* @author Samuel Roze <samuel.roze@gmail.com>
*
* @experimental in 4.2
* @deprecated since 4.3, pass a logger to SendMessageMiddleware instead
*/
class LoggingMiddleware implements MiddlewareInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Messenger\Middleware;

use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
use Symfony\Component\Messenger\Stamp\SentStamp;
Expand All @@ -24,31 +26,48 @@
*/
class SendMessageMiddleware implements MiddlewareInterface
{
use LoggerAwareTrait;

private $sendersLocator;

public function __construct(SendersLocatorInterface $sendersLocator)
{
$this->sendersLocator = $sendersLocator;
$this->logger = new NullLogger();
}

/**
* {@inheritdoc}
*/
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
if ($envelope->all(ReceivedStamp::class)) {
// it's a received message, do not send it back
return $stack->next()->handle($envelope, $stack);
}
$context = [
'message' => $envelope->getMessage(),
'class' => \get_class($envelope->getMessage()),
];

$handle = false;
$sender = null;

foreach ($this->sendersLocator->getSenders($envelope, $handle) as $alias => $sender) {
$envelope = $sender->send($envelope)->with(new SentStamp(\get_class($sender), \is_string($alias) ? $alias : null));
}
try {
if ($envelope->all(ReceivedStamp::class)) {
// it's a received message, do not send it back
$this->logger->info('Received message "{class}"', $context);
} else {
foreach ($this->sendersLocator->getSenders($envelope, $handle) as $alias => $sender) {
$this->logger->info('Sending message "{class}" with "{sender}"', $context + ['sender' => \get_class($sender)]);
$envelope = $sender->send($envelope)->with(new SentStamp(\get_class($sender), \is_string($alias) ? $alias : null));
}
}

if (null === $sender || $handle) {
return $stack->next()->handle($envelope, $stack);
}
} catch (\Throwable $e) {
$context['exception'] = $e;
$this->logger->warning('An exception occurred while handling message "{class}"', $context);

if (null === $sender || $handle) {
return $stack->next()->handle($envelope, $stack);
throw $e;
}

// message should only be sent and not be handled by the next middleware
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;

/**
* @group legacy
*/
class LoggingMiddlewareTest extends MiddlewareTestCase
{
public function testDebugLogAndNextMiddleware()
Expand Down
4 changes: 2 additions & 2 deletions 4 src/Symfony/Component/Messenger/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
}
],
"require": {
"php": "^7.1.3"
"php": "^7.1.3",
"psr/log": "~1.0"
},
"require-dev": {
"psr/log": "~1.0",
"symfony/console": "~3.4|~4.0",
"symfony/dependency-injection": "~3.4.19|^4.1.8",
"symfony/http-kernel": "~3.4|~4.0",
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.