Skip to content

Commit 09b4bae

Browse files
HypeMCGromNaN
authored andcommitted
Don't add processor to all loggers if tags specify a channel or handler
1 parent c64ef75 commit 09b4bae

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Migrate services configuration to PHP
77
* Add `console.interactive_only` flag
88
* Add `slack.exclude_fields` and `slackwebhook.exclude_fields` configuration
9+
* Add a processor to all loggers only when tags do not specify a channel or handler
910

1011
## 3.10.0 (2023-11-06)
1112

composer.json

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

src/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: 55 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,58 @@ public function testFailureOnHandlerWithoutPushProcessor()
6568
}
6669
}
6770

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

0 commit comments

Comments
 (0)