-
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add default_excluded_channels configuration option
- Loading branch information
Showing
7 changed files
with
340 additions
and
4 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
DependencyInjection/Compiler/DefaultExcludedChannelsPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]> | ||
|
@@ -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)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
203 changes: 203 additions & 0 deletions
203
Tests/DependencyInjection/Compiler/DefaultExcludedChannelsPassTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
); | ||
} | ||
} |