Skip to content
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

Symfony autowiring monolog channels #278 #315

Merged
merged 8 commits into from
Sep 2, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Added `sentry` type to use sentry 2.0 client
* Added `insightops` handler
* 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 = array('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 th monolog.logger_prototype
Seldaek marked this conversation as resolved.
Show resolved Hide resolved
*
* @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
25 changes: 19 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,20 @@ public function testProcess()
}
}

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

// test below acceptable only for Symfony 4.2+
Seldaek marked this conversation as resolved.
Show resolved Hide resolved
if (!\method_exists(ContainerBuilder::class, 'registerAliasForArgument')) {
return;
}

$expectedChannels = \array_keys($expected);
$expectedChannels[] = '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 +179,7 @@ private function getContainer()
$container->setDefinition($name, $service);
}

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

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

$container->getCompilerPassConfig()->setOptimizationPasses(array());
Expand Down