monolog-bundle icon indicating copy to clipboard operation
monolog-bundle copied to clipboard

WithMonologChannel on argument instead of the class

Open lyrixx opened this issue 1 year ago • 3 comments

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 ?

lyrixx avatar Feb 21 '24 21:02 lyrixx