Skip to content

[consumption] Add ability to overwrite logger. #289

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 2 commits into from
Dec 14, 2017
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
28 changes: 28 additions & 0 deletions docs/cookbook/symfony/how-to-change-consume-command-logger.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# How to change consume command logger

By default `bin/console enqueue:consume` (or `bin/console enqueue:transport:consume`) command prints messages to output.
The amount of info could be controlled by verbosity option (-v, -vv, -vvv).

In order to change the default logger used by a command you have to register a `LoggerExtension` just before the default one.
The extension asks you for a logger service, so just pass the one you want to use.
Here's how you can do it.

```yaml
// config/services.yaml

services:
app_logger_extension:
class: 'Enqueue\Consumption\Extension\LoggerExtension'
public: false
arguments: ['@logger']
tags:
- { name: 'enqueue.consumption.extension', priority: 255 }

```

The logger extension with the highest priority will set its logger.

[back to index](../index.md)



5 changes: 5 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
* [Development](#development)
- [Contribution](contribution.md)

## Cookbook

* [Symfony](#symfony-cookbook)
- [How to change consume command logger](cookbook/symfony/how-to-change-consume-command-logger.md)

# Blogs

* [Getting Started with RabbitMQ in PHP](https://blog.forma-pro.com/getting-started-with-rabbitmq-in-php-84d331e20a66)
Expand Down
12 changes: 10 additions & 2 deletions pkg/enqueue/Consumption/Extension/LoggerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ public function __construct(LoggerInterface $logger)
*/
public function onStart(Context $context)
{
$context->setLogger($this->logger);
$this->logger->debug(sprintf('Set context\'s logger %s', get_class($this->logger)));
if ($context->getLogger()) {
$context->getLogger()->debug(sprintf(
'Skip setting context\'s logger "%s". Another one "%s" has already been set.',
get_class($context->getLogger()),
get_class($this->logger)
));
} else {
$context->setLogger($this->logger);
$this->logger->debug(sprintf('Set context\'s logger "%s"', get_class($this->logger)));
}
}

/**
Expand Down
23 changes: 23 additions & 0 deletions pkg/enqueue/Tests/Consumption/Extension/LoggerExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,29 @@ public function testShouldNotLogAckMessageStatusIfReasonIsEmpty()
$extension->onPostReceived($context);
}

public function testShouldNotSetLoggerIfOneHasBeenSetOnStart()
{
$logger = $this->createLogger();

$alreadySetLogger = $this->createLogger();
$alreadySetLogger
->expects($this->once())
->method('debug')
->with(sprintf(
'Skip setting context\'s logger "%s". Another one "%s" has already been set.',
get_class($logger),
get_class($alreadySetLogger)
))
;

$extension = new LoggerExtension($logger);

$context = new Context($this->createPsrContextMock());
$context->setLogger($alreadySetLogger);

$extension->onStart($context);
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|PsrContext
*/
Expand Down