Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the #[WithMonologChannel] attribute of Monolog 3.5 to autoconfigure the channel #468

Merged
merged 1 commit into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
* Add support for env placeholders in the `level` option of handlers
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 @@ -135,6 +136,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