Skip to content

Commit

Permalink
Merge pull request #315 from adrenalinkin/autowiring_monolog_channels
Browse files Browse the repository at this point in the history
Symfony autowiring monolog channels #278
  • Loading branch information
Seldaek committed Sep 2, 2019
2 parents 3239d22 + 9fc40dc commit fabf119
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
## 3.5.0 (xxxx-xx-xx)

* Added support for Monolog 2.0
* Added `sentry` type to use sentry 2.0 client
* Added `insightops` handler
* Added support for Monolog 2.0
* Added possibility for auto-wire monolog channel according to the type-hinted aliases, introduced in the Symfony 4.2

## 3.4.0 (2019-06-20)

Expand Down
31 changes: 28 additions & 3 deletions DependencyInjection/Compiler/LoggerChannelPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

namespace Symfony\Bundle\MonologBundle\DependencyInjection\Compiler;

use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;

/**
* Replaces the default logger by another one with its own channel for tagged services.
Expand All @@ -28,6 +28,9 @@ class LoggerChannelPass implements CompilerPassInterface
{
protected $channels = ['app'];

/**
* {@inheritDoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('monolog.logger')) {
Expand Down Expand Up @@ -105,11 +108,19 @@ public function process(ContainerBuilder $container)
}
}

/**
* @return array
*/
public function getChannels()
{
return $this->channels;
}

/**
* @param array $configuration
*
* @return array
*/
protected function processChannels($configuration)
{
if (null === $configuration) {
Expand All @@ -123,6 +134,13 @@ protected function processChannels($configuration)
return array_diff($this->channels, $configuration['elements']);
}

/**
* Create new logger from the monolog.logger_prototype
*
* @param string $channel
* @param string $loggerId
* @param ContainerBuilder $container
*/
protected function createLogger($channel, $loggerId, ContainerBuilder $container)
{
if (!in_array($channel, $this->channels)) {
Expand All @@ -131,6 +149,13 @@ protected function createLogger($channel, $loggerId, ContainerBuilder $container
$container->setDefinition($loggerId, $logger);
$this->channels[] = $channel;
}

// Allows only for Symfony 4.2+
if (\method_exists($container, 'registerAliasForArgument')) {
$parameterName = $channel . 'Logger';

$container->registerAliasForArgument($loggerId, LoggerInterface::class, $parameterName);
}
}

/**
Expand Down
27 changes: 21 additions & 6 deletions Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;

class LoggerChannelPassTest extends TestCase
{
Expand Down Expand Up @@ -51,7 +51,22 @@ public function testProcess()
}
}

$this->assertNotNull($container->getDefinition('monolog.logger.manualchan'));
$this->assertNotNull($container->getDefinition('monolog.logger.additional'));
}

public function testTypeHintedAliasesExistForEachChannel()
{
if (!\method_exists(ContainerBuilder::class, 'registerAliasForArgument')) {
$this->markTestSkipped('Need DependencyInjection 4.2+ to register type-hinted aliases for channels.');
}

$container = $this->getContainer();
$expectedChannels = ['test', 'foo', 'bar', 'additional'];

foreach ($expectedChannels as $channelName) {
$aliasName = LoggerInterface::class.' $' .$channelName.'Logger';
$this->assertTrue($container->hasAlias($aliasName), 'type-hinted alias should be exists for each logger channel');
}
}

public function testProcessSetters()
Expand Down Expand Up @@ -166,7 +181,7 @@ private function getContainer()
$container->setDefinition($name, $service);
}

$container->setParameter('monolog.additional_channels', ['manualchan']);
$container->setParameter('monolog.additional_channels', ['additional']);
$container->setParameter('monolog.handlers_to_channels', [
'monolog.handler.a' => [
'type' => 'inclusive',
Expand Down Expand Up @@ -202,7 +217,7 @@ private function getContainerWithSetter()
$service->addMethodCall('setLogger', [new Reference('logger')]);
$container->setDefinition('foo', $service);

$container->setParameter('monolog.additional_channels', ['manualchan']);
$container->setParameter('monolog.additional_channels', ['additional']);
$container->setParameter('monolog.handlers_to_channels', []);

$container->getCompilerPassConfig()->setOptimizationPasses([]);
Expand Down

0 comments on commit fabf119

Please sign in to comment.