Skip to content

Commit

Permalink
Add default_excluded_channels configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC committed Mar 13, 2023
1 parent 0e136c5 commit f5cdcad
Show file tree
Hide file tree
Showing 7 changed files with 340 additions and 4 deletions.
79 changes: 79 additions & 0 deletions DependencyInjection/Compiler/DefaultExcludedChannelsPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MonologBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Excludes all specified channels from handlers with a non-exclusive channel list.
* Needs to run before {@see LoggerChannelPass}.
*/
class DefaultExcludedChannelsPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if ($defaultExcludedChannels = array_values($container->getParameter('monolog.default_excluded_channels'))) {
$this->processChannels($container, $defaultExcludedChannels);
}

$container->getParameterBag()->remove('monolog.default_excluded_channels');
}

private function processChannels(ContainerBuilder $container, array $defaultExcludedChannels): void
{
$processedHandlers = [];

/** @var array<string, ?array{type: string, elements: list<string>}> $handlersToChannels */
$handlersToChannels = $container->getParameter('monolog.handlers_to_channels');

foreach ($handlersToChannels as $id => &$handlersToChannel) {
if (isset($handlersToChannel['type']) && 'exclusive' !== $handlersToChannel['type']) {
continue;
}

$handlerName = substr($id, 16); // remove "monolog.handler."

if (null === $handlersToChannel) {
$handlersToChannel = [
'type' => 'exclusive',
'elements' => $defaultExcludedChannels,
];
$processedHandlers[$handlerName] = $defaultExcludedChannels;

continue;
}

foreach ($defaultExcludedChannels as $defaultExcludedChannel) {
if (false !== $index = array_search('!'.$defaultExcludedChannel, $handlersToChannel['elements'], true)) {
array_splice($handlersToChannel['elements'], $index, 1);
if (!$handlersToChannel['elements']) {
$handlersToChannel = null;
}
} elseif (!\in_array($defaultExcludedChannel, $handlersToChannel['elements'], true)) {
$handlersToChannel['elements'][] = $defaultExcludedChannel;
$processedHandlers[$handlerName][] = $defaultExcludedChannel;
}
}
}

$container->setParameter('monolog.handlers_to_channels', $handlersToChannels);

foreach ($processedHandlers as $handlerName => $excludedChannels) {
$container->log($this, sprintf(
'Excluded the following default excluded channels from the "%s" handler: "%s".',
$handlerName,
implode('", "', $excludedChannels)
));
}
}
}
5 changes: 5 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,18 @@ public function getConfigTreeBuilder()

$handlers = $rootNode
->fixXmlConfig('channel')
->fixXmlConfig('default_excluded_channel')
->fixXmlConfig('handler')
->children()
->scalarNode('use_microseconds')->defaultTrue()->end()
->arrayNode('channels')
->canBeUnset()
->prototype('scalar')->end()
->end()
->arrayNode('default_excluded_channels')
->canBeUnset()
->prototype('scalar')->end()
->end()
->arrayNode('handlers');

$handlers
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public function load(array $configs, ContainerBuilder $container)
}

$container->setParameter('monolog.additional_channels', isset($config['channels']) ? $config['channels'] : []);
$container->setParameter('monolog.default_excluded_channels', $config['default_excluded_channels'] ?? []);

if (method_exists($container, 'registerForAutoconfiguration')) {
if (interface_exists(ProcessorInterface::class)) {
Expand Down
11 changes: 7 additions & 4 deletions MonologBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
use Monolog\Formatter\JsonFormatter;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\HandlerInterface;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\AddProcessorsPass;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\AddSwiftMailerTransportPass;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\DebugHandlerPass;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\AddProcessorsPass;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\DefaultExcludedChannelsPass;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\FixEmptyLoggerPass;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* @author Jordi Boggiano <[email protected]>
Expand All @@ -31,6 +33,7 @@ public function build(ContainerBuilder $container)
{
parent::build($container);

$container->addCompilerPass(new DefaultExcludedChannelsPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
$container->addCompilerPass($channelPass = new LoggerChannelPass());
if (!class_exists('Symfony\Bridge\Monolog\Processor\DebugProcessor') || !class_exists('Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddDebugLogProcessorPass')) {
$container->addCompilerPass(new DebugHandlerPass($channelPass));
Expand Down
1 change: 1 addition & 0 deletions Resources/config/schema/monolog-1.0.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="handler" type="handler" />
<xsd:element name="channel" type="xsd:string" />
<xsd:element name="default-excluded-channel" type="xsd:string" />
</xsd:choice>
</xsd:complexType>

Expand Down
203 changes: 203 additions & 0 deletions Tests/DependencyInjection/Compiler/DefaultExcludedChannelsPassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Compiler;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\DefaultExcludedChannelsPass;
use Symfony\Bundle\MonologBundle\MonologBundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class DefaultExcludedChannelsPassTest extends TestCase
{
/**
* @group legacy
*
* @dataProvider handlerChannels
*/
public function testProcess(?array $defaultExcludedChannels, array $handlers, ?array $expectedChannels, array $expectedLog): void
{
$container = new ContainerBuilder();

$bundle = new MonologBundle();
$container->registerExtension($bundle->getContainerExtension());
$bundle->build($container);

$container->loadFromExtension('monolog', [
'channels' => ['channel1', 'channel2', 'channel3', 'channel4'],
'default_excluded_channels' => $defaultExcludedChannels,
'handlers' => $handlers,
]);

$container->compile();

$this->assertSame($expectedChannels, $container->getParameter('monolog.handlers_to_channels'));
$this->assertFalse($container->hasParameter('monolog.default_excluded_channels'));

$this->assertSame($expectedLog, array_values(array_filter(
$container->getCompiler()->getLog(),
function (string $log) {
return 0 === strpos($log, DefaultExcludedChannelsPass::class);
}
)));
}

public static function handlerChannels(): iterable
{
yield 'No default excluded channels' => [
null,
[
'foo' => [
'type' => 'console',
'channels' => ['!channel1'],
],
],
['monolog.handler.foo' => ['type' => 'exclusive', 'elements' => ['channel1']]],
[],
];
yield 'Empty default excluded channels array' => [
[],
[
'foo' => [
'type' => 'console',
'channels' => ['!channel1'],
],
],
['monolog.handler.foo' => ['type' => 'exclusive', 'elements' => ['channel1']]],
[],
];

yield 'No channels' => [
['channel1'],
[
'foo' => [
'type' => 'console',
'channels' => null,
],
],
['monolog.handler.foo' => ['type' => 'exclusive', 'elements' => ['channel1']]],
[self::getLog('foo', ['channel1'])],
];
yield 'Empty channels array' => [
['channel1'],
[
'foo' => [
'type' => 'console',
'channels' => [],
],
],
['monolog.handler.foo' => ['type' => 'exclusive', 'elements' => ['channel1']]],
[self::getLog('foo', ['channel1'])],
];

yield 'Inclusive' => [
['channel2'],
[
'foo' => [
'type' => 'console',
'channels' => ['channel1'],
],
],
['monolog.handler.foo' => ['type' => 'inclusive', 'elements' => ['channel1']]],
[],
];

yield 'Exclusive without exception' => [
['channel2'],
[
'foo' => [
'type' => 'console',
'channels' => ['!channel1'],
],
],
['monolog.handler.foo' => ['type' => 'exclusive', 'elements' => ['channel1', 'channel2']]],
[self::getLog('foo', ['channel2'])],
];
yield 'Exclusive with exception' => [
['channel2'],
[
'foo' => [
'type' => 'console',
'channels' => ['!channel1', '!!channel2'],
],
],
['monolog.handler.foo' => ['type' => 'exclusive', 'elements' => ['channel1']]],
[],
];
yield 'Exclusive with only an exception' => [
['channel1'],
[
'foo' => [
'type' => 'console',
'channels' => ['!!channel1'],
],
],
['monolog.handler.foo' => null],
[],
];

yield 'Explicitly excluded' => [
['channel1'],
[
'foo' => [
'type' => 'console',
'channels' => ['!channel1'],
],
],
['monolog.handler.foo' => ['type' => 'exclusive', 'elements' => ['channel1']]],
[],
];

yield 'Multiple default excluded channels' => [
['channel1', 'channel3'],
[
'foo' => [
'type' => 'console',
'channels' => ['!channel2'],
],
],
['monolog.handler.foo' => ['type' => 'exclusive', 'elements' => ['channel2', 'channel1', 'channel3']]],
[self::getLog('foo', ['channel1', 'channel3'])],
];

yield 'Multiple handlers' => [
['channel1', 'channel3'],
[
'foo' => [
'type' => 'console',
'channels' => ['!channel2'],
],
'bar' => [
'type' => 'console',
'channels' => ['channel1', 'channel4'],
],
'baz' => [
'type' => 'console',
'channels' => ['!!channel1', '!channel2'],
],
],
[
'monolog.handler.baz' => ['type' => 'exclusive', 'elements' => ['channel2', 'channel3']],
'monolog.handler.bar' => ['type' => 'inclusive', 'elements' => ['channel1', 'channel4']],
'monolog.handler.foo' => ['type' => 'exclusive', 'elements' => ['channel2', 'channel1', 'channel3']],
],
[
self::getLog('baz', ['channel3']),
self::getLog('foo', ['channel1', 'channel3']),
],
];
}

private static function getLog(string $handler, array $channels): string
{
return sprintf('%s: Excluded the following default excluded channels from the "%s" handler: "%s".', DefaultExcludedChannelsPass::class, $handler, implode('", "', $channels));
}
}
44 changes: 44 additions & 0 deletions Tests/MonologBundleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MonologBundle\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\DefaultExcludedChannelsPass;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
use Symfony\Bundle\MonologBundle\MonologBundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class MonologBundleTest extends TestCase
{
/**
* @group legacy
*/
public function testDefaultExcludedChannelsPassIsRegisteredWithCorrectPriority()
{
$container = new ContainerBuilder();

(new MonologBundle())->build($container);

$compilerPassIndexes = [];
foreach ($container->getCompilerPassConfig()->getBeforeOptimizationPasses() as $i => $compilerPass) {
$compilerPassIndexes[\get_class($compilerPass)] = $i;
}

$this->assertArrayHasKey(LoggerChannelPass::class, $compilerPassIndexes);
$this->assertArrayHasKey(DefaultExcludedChannelsPass::class, $compilerPassIndexes);

$this->assertGreaterThan(
$compilerPassIndexes[DefaultExcludedChannelsPass::class],
$compilerPassIndexes[LoggerChannelPass::class]
);
}
}

0 comments on commit f5cdcad

Please sign in to comment.