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 e9e0f98
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
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 <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\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

0 comments on commit e9e0f98

Please sign in to comment.