Skip to content
This repository has been archived by the owner on Jan 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #42 from Taluu/set-logger
Browse files Browse the repository at this point in the history
Dynamically change the logger
  • Loading branch information
Taluu authored Jan 5, 2018
2 parents 4419939 + f132c1e commit 0935575
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ private function addAmqpNode(NodeDefinition $root)
->isRequired()
->end()

->scalarNode('logger_channel')
->info('Logger channel to use when a logger is required')
->defaultValue('amqp')
->end()

->arrayNode('connections')
->info('Connections to AMQP to use')
->useAttributeAsKey('name')
Expand Down
14 changes: 14 additions & 0 deletions src/DependencyInjection/WisemblyAmqpExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use Wisembly\AmqpBundle\Broker\PeclBroker;

use Wisembly\AmqpBundle\Processor\ProcessFactory;
use Wisembly\AmqpBundle\Processor\ConsumerFactory;
use Wisembly\AmqpBundle\Processor\CommandProcessor;

/**
* This is the class that loads and manages your bundle configuration
Expand Down Expand Up @@ -59,6 +61,18 @@ public function load(array $configs, ContainerBuilder $container)
$container->registerForAutoconfiguration(BrokerInterface::class)
->addTag('wisembly.amqp.broker')
;

// dynamically change the monolog.logger tag
foreach ([ConsumerFactory::class, CommandProcessor::class] as $service) {
$def = $container->getDefinition($service);
$tags = $def->getTag('monolog.logger');
$def->clearTag('monolog.logger');

foreach ($tags as $attributes) {
$attributes['channel'] = $config['logger_channel'];
$def->addTag('monolog.logger', $attributes);
}
}
}

public function getConfig(array $configs): array
Expand Down
22 changes: 21 additions & 1 deletion tests/DependencyInjection/WisemblyAmqpExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
use Wisembly\AmqpBundle\GatesBag;
use Wisembly\AmqpBundle\Connection;
use Wisembly\AmqpBundle\UriConnection;

use Wisembly\AmqpBundle\BrokerInterface;
use Wisembly\AmqpBundle\Broker\PeclBroker;

use Wisembly\AmqpBundle\Processor\ConsumerFactory;
use Wisembly\AmqpBundle\Processor\CommandProcessor;

class WisemblyAmqpExtensionTest extends TestCase
{
public function test_it_can_be_instantiated()
Expand Down Expand Up @@ -116,7 +120,23 @@ public function test_it_picks_the_default_connection_if_none_specified()
}
}

private function getContainer(array $config = [])
public function test_it_changes_the_monolog_tag()
{
$container = $this->getContainer([
'logger_channel' => 'foo'
]);

foreach ([CommandProcessor::class, ConsumerFactory::class] as $service) {
$def = $container->getDefinition($service);
$tags = $def->getTag('monolog.logger');

foreach ($tags as $attributes) {
$this->assertSame('foo', $attributes['channel']);
}
}
}

private function getContainer(array $config = []): ContainerBuilder
{
$broker = $this->prophesize(BrokerInterface::class)->reveal();

Expand Down

0 comments on commit 0935575

Please sign in to comment.