Skip to content

Commit

Permalink
Merge pull request #468 from stof/channel_attribute
Browse files Browse the repository at this point in the history
Add support for the `#[WithMonologChannel]` attribute of Monolog 3.5 to autoconfigure the channel
  • Loading branch information
stof authored Oct 28, 2023
2 parents 2a96e4f + 0669745 commit b98138a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Unreleased

* Add support for the `WithMonologChannel` attribute of Monolog 3.5.0 to autoconfigure the `monolog.logger` tag
* Add support for Symfony 7
* Remove support for Symfony 4
* Mark classes as internal when relevant
Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\MonologBundle\DependencyInjection;

use Monolog\Attribute\AsMonologProcessor;
use Monolog\Attribute\WithMonologChannel;
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Monolog\Handler\HandlerInterface;
use Monolog\Logger;
Expand Down Expand Up @@ -137,6 +138,9 @@ public function load(array $configs, ContainerBuilder $container)

$definition->addTag('monolog.processor', $tagAttributes);
});
$container->registerAttributeForAutoconfiguration(WithMonologChannel::class, static function (ChildDefinition $definition, WithMonologChannel $attribute): void {
$definition->addTag('monolog.logger', ['channel' => $attribute->channel]);
});
}
}

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

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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\Fixtures;

use Monolog\Attribute\WithMonologChannel;

#[WithMonologChannel('fixture')]
class ServiceWithChannel
{
}
23 changes: 22 additions & 1 deletion Tests/DependencyInjection/MonologExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@

use InvalidArgumentException;
use Monolog\Attribute\AsMonologProcessor;
use Monolog\Attribute\WithMonologChannel;
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Monolog\Handler\RollbarHandler;
use Monolog\Logger;
use Monolog\Processor\UidProcessor;
use Symfony\Bridge\Monolog\Processor\SwitchUserTokenProcessor;
use Symfony\Bundle\MonologBundle\DependencyInjection\MonologExtension;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\FooProcessor;
use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\FooProcessorWithPriority;
use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\RedeclareMethodProcessor;
use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\ServiceWithChannel;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
Expand Down Expand Up @@ -837,6 +838,26 @@ public function testAsMonologProcessorAutoconfigurationWithPriority(): void
], $container->getDefinition(FooProcessorWithPriority::class)->getTag('monolog.processor'));
}

/**
* @requires PHP 8.0
*/
public function testWithLoggerChannelAutoconfiguration(): void
{
if (!class_exists(WithMonologChannel::class)) {
$this->markTestSkipped('Monolog >= 3.5.0 is needed.');
}

$container = $this->getContainer([], [
ServiceWithChannel::class => (new Definition(ServiceWithChannel::class))->setAutoconfigured(true),
]);

$this->assertSame([
[
'channel' => 'fixture',
],
], $container->getDefinition(ServiceWithChannel::class)->getTag('monolog.logger'));
}

protected function getContainer(array $config = [], array $thirdPartyDefinitions = []): ContainerBuilder
{
$container = new ContainerBuilder(new EnvPlaceholderParameterBag());
Expand Down

0 comments on commit b98138a

Please sign in to comment.