Skip to content

Commit 0669745

Browse files
committed
Add support for the WithMonologChannel attribute of Monolog 3.5
1 parent 110a839 commit 0669745

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Unreleased
22

3+
* Add support for the `WithMonologChannel` attribute of Monolog 3.5.0 to autoconfigure the `monolog.logger` tag
34
* Add support for Symfony 7
45
* Remove support for Symfony 4
56
* Add support for env placeholders in the `level` option of handlers

DependencyInjection/MonologExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\MonologBundle\DependencyInjection;
1313

1414
use Monolog\Attribute\AsMonologProcessor;
15+
use Monolog\Attribute\WithMonologChannel;
1516
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
1617
use Monolog\Handler\HandlerInterface;
1718
use Monolog\Logger;
@@ -135,6 +136,9 @@ public function load(array $configs, ContainerBuilder $container)
135136

136137
$definition->addTag('monolog.processor', $tagAttributes);
137138
});
139+
$container->registerAttributeForAutoconfiguration(WithMonologChannel::class, static function (ChildDefinition $definition, WithMonologChannel $attribute): void {
140+
$definition->addTag('monolog.logger', ['channel' => $attribute->channel]);
141+
});
138142
}
139143
}
140144

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures;
13+
14+
use Monolog\Attribute\WithMonologChannel;
15+
16+
#[WithMonologChannel('fixture')]
17+
class ServiceWithChannel
18+
{
19+
}

Tests/DependencyInjection/MonologExtensionTest.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@
1313

1414
use InvalidArgumentException;
1515
use Monolog\Attribute\AsMonologProcessor;
16+
use Monolog\Attribute\WithMonologChannel;
1617
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
1718
use Monolog\Handler\RollbarHandler;
1819
use Monolog\Logger;
1920
use Monolog\Processor\UidProcessor;
20-
use Symfony\Bridge\Monolog\Processor\SwitchUserTokenProcessor;
2121
use Symfony\Bundle\MonologBundle\DependencyInjection\MonologExtension;
2222
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
2323
use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\FooProcessor;
2424
use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\FooProcessorWithPriority;
2525
use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\RedeclareMethodProcessor;
26+
use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\ServiceWithChannel;
2627
use Symfony\Component\DependencyInjection\ContainerBuilder;
2728
use Symfony\Component\DependencyInjection\Definition;
2829
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
@@ -837,6 +838,26 @@ public function testAsMonologProcessorAutoconfigurationWithPriority(): void
837838
], $container->getDefinition(FooProcessorWithPriority::class)->getTag('monolog.processor'));
838839
}
839840

841+
/**
842+
* @requires PHP 8.0
843+
*/
844+
public function testWithLoggerChannelAutoconfiguration(): void
845+
{
846+
if (!class_exists(WithMonologChannel::class)) {
847+
$this->markTestSkipped('Monolog >= 3.5.0 is needed.');
848+
}
849+
850+
$container = $this->getContainer([], [
851+
ServiceWithChannel::class => (new Definition(ServiceWithChannel::class))->setAutoconfigured(true),
852+
]);
853+
854+
$this->assertSame([
855+
[
856+
'channel' => 'fixture',
857+
],
858+
], $container->getDefinition(ServiceWithChannel::class)->getTag('monolog.logger'));
859+
}
860+
840861
protected function getContainer(array $config = [], array $thirdPartyDefinitions = []): ContainerBuilder
841862
{
842863
$container = new ContainerBuilder(new EnvPlaceholderParameterBag());

0 commit comments

Comments
 (0)