Skip to content

Commit b8070aa

Browse files
committed
Don't add processor to all loggers if tags specify a channel or handler
1 parent a3834c1 commit b8070aa

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

DependencyInjection/Compiler/AddProcessorsPass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public function process(ContainerBuilder $container)
3232
}
3333

3434
foreach ($container->findTaggedServiceIds('monolog.processor') as $id => $tags) {
35+
if (array_any($tags, $closure = function (array $tag) { return (bool) $tag; })) {
36+
$tags = array_filter($tags, $closure);
37+
}
38+
3539
foreach ($tags as $tag) {
3640
if (!empty($tag['channel']) && !empty($tag['handler'])) {
3741
throw new \InvalidArgumentException(\sprintf('you cannot specify both the "handler" and "channel" attributes for the "monolog.processor" tag on service "%s"', $id));

Tests/DependencyInjection/Compiler/AddProcessorsPassTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
use PHPUnit\Framework\TestCase;
1717
use Symfony\Bridge\Monolog\Handler\ConsoleHandler;
1818
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\AddProcessorsPass;
19+
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
1920
use Symfony\Component\Config\FileLocator;
21+
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
22+
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
2023
use Symfony\Component\DependencyInjection\ContainerBuilder;
2124
use Symfony\Component\DependencyInjection\Definition;
2225
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
@@ -65,6 +68,61 @@ public function testFailureOnHandlerWithoutPushProcessor()
6568
}
6669
}
6770

71+
/**
72+
* @dataProvider provideEmptyTagsData
73+
*/
74+
public function testEmptyTagsAreIgnoredWhenNonEmptyArePresent(
75+
array $tagAttributesList,
76+
array $expectedLoggerCalls,
77+
array $expectedMyChannelLoggerCalls
78+
) {
79+
$container = new ContainerBuilder();
80+
$loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config'));
81+
$loader->load('monolog.php');
82+
83+
$container->setParameter('monolog.additional_channels', ['my_channel']);
84+
$container->setParameter('monolog.handlers_to_channels', []);
85+
86+
$container->register('TestClass')->setTags(['monolog.processor' => $tagAttributesList]);
87+
88+
$container->getCompilerPassConfig()->setOptimizationPasses([]);
89+
$container->getCompilerPassConfig()->setRemovingPasses([]);
90+
$container->addCompilerPass(new ResolveChildDefinitionsPass(), PassConfig::TYPE_OPTIMIZE);
91+
$container->addCompilerPass(new LoggerChannelPass());
92+
$container->addCompilerPass(new AddProcessorsPass());
93+
$container->compile();
94+
95+
$this->assertEquals($expectedLoggerCalls, $container->getDefinition('monolog.logger')->getMethodCalls());
96+
$this->assertEquals($expectedMyChannelLoggerCalls, $container->getDefinition('monolog.logger.my_channel')->getMethodCalls());
97+
}
98+
99+
public static function provideEmptyTagsData(): iterable
100+
{
101+
yield 'with empty tag' => [
102+
[[]],
103+
[['pushProcessor', [new Reference('TestClass')]], ['useMicrosecondTimestamps', ['%monolog.use_microseconds%']]],
104+
[['pushProcessor', [new Reference('TestClass')]]],
105+
];
106+
107+
yield 'with app channel' => [
108+
[[], ['channel' => 'app']],
109+
[['useMicrosecondTimestamps', ['%monolog.use_microseconds%']], ['pushProcessor', [new Reference('TestClass')]]],
110+
[],
111+
];
112+
113+
yield 'with my_channel channel' => [
114+
[[], ['channel' => 'my_channel']],
115+
[['useMicrosecondTimestamps', ['%monolog.use_microseconds%']]],
116+
[['pushProcessor', [new Reference('TestClass')]]],
117+
];
118+
119+
yield 'with method and no channel' => [
120+
[[], ['method' => 'foo']],
121+
[['pushProcessor', [[new Reference('TestClass'), 'foo']]], ['useMicrosecondTimestamps', ['%monolog.use_microseconds%']]],
122+
[['pushProcessor', [[new Reference('TestClass'), 'foo']]]],
123+
];
124+
}
125+
68126
protected function getContainer()
69127
{
70128
$container = new ContainerBuilder();

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"symfony/config": "^5.4 || ^6.0 || ^7.0",
2222
"symfony/dependency-injection": "^5.4 || ^6.0 || ^7.0",
2323
"symfony/http-kernel": "^5.4 || ^6.0 || ^7.0",
24-
"symfony/monolog-bridge": "^5.4 || ^6.0 || ^7.0"
24+
"symfony/monolog-bridge": "^5.4 || ^6.0 || ^7.0",
25+
"symfony/polyfill-php84": "^1.30"
2526
},
2627
"require-dev": {
2728
"symfony/console": "^5.4 || ^6.0 || ^7.0",

0 commit comments

Comments
 (0)