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

WithMonologChannel on argument instead of the class #475

Open
lyrixx opened this issue Feb 21, 2024 · 3 comments
Open

WithMonologChannel on argument instead of the class #475

lyrixx opened this issue Feb 21, 2024 · 3 comments

Comments

@lyrixx
Copy link
Member

lyrixx commented Feb 21, 2024

Hello,

I was playing with #[WithMonologChannel] attribute. Without reading the doc, I put it on the constructor parameter, because I thought it would work like #[Target], #[Autowire], ... attributes

class HomepageController extends AbstractController
{
    public function __construct(
        #[WithMonologChannel('homepage')]
        private readonly LoggerInterface $logger,
        private readonly MessageBusInterface $bus,
    ) {
    }

But, what a surprise, it didn't work.

Instead it must be set on the class

#[WithMonologChannel('homepage')]
class HomepageController extends AbstractController
{
    public function __construct(
        private readonly LoggerInterface $logger,
        private readonly MessageBusInterface $bus,
    ) {
    }

What about allowing it on the parameter instead ? It feels more natural. And it allows having N different logger

class HomepageController extends AbstractController
{
    public function __construct(
        #[WithMonologChannel('homepage')]
        private readonly LoggerInterface $logger,
        #[WithMonologChannel('billing')]
        private readonly LoggerInterface $billingLogger,
        private readonly MessageBusInterface $bus,
    ) {
    }

WDYT ?

@Seldaek
Copy link
Member

Seldaek commented Feb 22, 2024

Makes sense but it'll require a tweak in monolog I guess.. as that only has target_class

@stof
Copy link
Member

stof commented Aug 5, 2024

it also require changing entirely the way the processing is implemented. Right now, this attribute only applies the tag on the service definition, which then defines a binding.

To support separate channels in different arguments, it would require hooking directly in the autowiring logic instead.
Can you try using the Target attribute of the DI component instead ? I think it might work already for known channels thanks to the registration done in

$container->registerAliasForArgument($loggerId, LoggerInterface::class, $parameterName);
(channels become known either because of a monolog.channel tag asking them to be created or because of the dedicated channels setting in the bundle configuration)

@lyrixx
Copy link
Member Author

lyrixx commented Aug 5, 2024

I used Target indeed 👍🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants