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 5d23509
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Attribute/WithLoggerChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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;

#[\Attribute(\Attribute::TARGET_CLASS)]
class WithLoggerChannel
{
private string $channel;

public function __construct(string $channel)
{
$this->channel = $channel;
}

public function getChannel(): string
{
return $this->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->getChannel()]);
});
}
}

Expand Down

0 comments on commit 5d23509

Please sign in to comment.