Gaëtan Lickel
Gaëtan Lickel

Reputation: 3

How override default Psr\LogLoggerInterface alias

I try to create an alias with id 'Psr\Log\LoggerInterface' for a service which implement this interface.
But the alias seems to not work.

Here is the my configuration in services.yaml

services:
    A\Name\Space\LoggerService:
        arguments:
            $arg1: ''
            $arg2: ''

    Psr\Log\LoggerInterface:
        alias: A\Name\Space\LoggerService

This is how I try to get my service in my controller:

class SomeController extends AbstractController
{
    public function index(LoggerInterface $logger): Response
    {
        $logger->info('index');
        return $this->render('index.html.twig');
    }
}

I expect to get a service with class A\Name\Space\LoggerService but I get the class ‌Symfony\Component\HttpKernel\Log\Logger.

If I get the service directly, it works well:

class SomeController extends AbstractController
{
    public function index(\A\Name\Space\LoggerService $logger): Response
    {
        $logger->info('index');
        return $this->render('index.html.twig');
    }
}

Finally, if I change my alias to:

logger:
    alias: A\Name\Space\LoggerService

it works well. But I don't want to override the Symfony logger service, only the alias.

Upvotes: 0

Views: 621

Answers (1)

stephan.mada
stephan.mada

Reputation: 1130

How about binding like this one :

services:
    _defaults:
        bind:
            #...
            Psr\Log\LoggerInterface $logger: '@A\Name\Space\LoggerService'

https://symfony.com/doc/current/service_container.html#binding-arguments-by-name-or-type

Upvotes: 2

Related Questions