Skip to content

Logger dependency injection by type not working #33531

@ghost

Description

Symfony version(s) affected: 4.3.4

Description
In Symfony 4.2 the logger service could be injected by type. After upgrading to 4.3.4 I can only inject it by using the argument name.

How to reproduce

// MyClass.php

namespace App\MyNamespace;

use Psr\Log\LoggerInterface;

class MyClass
{
    /**
     * @var LoggerInterface
     */
    private $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }
}
# services.yaml

services:

    App\MyNamespace\MyClass:
        arguments:
            Psr\Log\LoggerInterface: '@monolog.logger.my_channel'

In Symfony 4.3.4 I get the following error message:

In ResolveNamedArgumentsPass.php line 57:
Invalid service "App\MyNamespace\MyClass": did you forget to add the "$" prefix to argument "Psr\Log\LoggerInterface"?

It can be fixed by using a the argument name in services.yaml:

# services.yaml

services:

    App\MyNamespace\MyClass:
        arguments:
            $logger: '@monolog.logger.my_channel'

I wonder why the injection by type does not work anymore.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions