Skip to content

Commit

Permalink
Add the WithLoggerChannel attribute to autoconfigure the channel
Browse files Browse the repository at this point in the history
  • Loading branch information
stof committed Oct 24, 2023
1 parent 110a839 commit 618f2e6
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
31 changes: 31 additions & 0 deletions Attribute/WithLoggerChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MonologBundle\Attribute;

/**
* Autoconfigures the logger channel used when inject a logger in the service.
*
* This adds the `monolog.logger` tag on the service definition.
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
final class WithLoggerChannel
{
/**
* @readonly
*/
public string $channel;

public function __construct(string $channel)
{
$this->channel = $channel;
}
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Unreleased

* Add a `WithLoggerChannel` attribute 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 @@ -24,6 +24,7 @@
use Symfony\Bridge\Monolog\Processor\WebProcessor;
use Symfony\Bridge\Monolog\Logger as LegacyLogger;
use Symfony\Bundle\FullStack;
use Symfony\Bundle\MonologBundle\Attribute\WithLoggerChannel;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
use Symfony\Component\DependencyInjection\ChildDefinition;
Expand Down Expand Up @@ -135,6 +136,9 @@ public function load(array $configs, ContainerBuilder $container)

$definition->addTag('monolog.processor', $tagAttributes);
});
$container->registerAttributeForAutoconfiguration(WithLoggerChannel::class, static function (ChildDefinition $definition, WithLoggerChannel $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 <[email protected]>
*
* 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 Symfony\Bundle\MonologBundle\Attribute\WithLoggerChannel;

#[WithLoggerChannel('fixture')]
class ServiceWithChannel
{
}
18 changes: 17 additions & 1 deletion Tests/DependencyInjection/MonologExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
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 +837,22 @@ public function testAsMonologProcessorAutoconfigurationWithPriority(): void
], $container->getDefinition(FooProcessorWithPriority::class)->getTag('monolog.processor'));
}

/**
* @requires PHP 8.0
*/
public function testWithLoggerChannelAutoconfiguration(): void
{
$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 618f2e6

Please sign in to comment.