I have recently upgraded from Symfony 3.4 to 4.4
The codebase is really large and there are a lot of references to container->get('logger') from all over the place.
I have been trying to make this service public, but not having any luck with any examples I can find. Error:
The "logger" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.
In my service.yml I have tried this with no luck:
Psr\Log\LoggerInterface:
public: true
Psr\Log\LoggerInterface:
alias: 'logger'
public: true
Psr\Log\LoggerInterface:
alias: 'monolog.logger'
public: true
How can I make the logger service public?
NOTE: in the upgrade from 3.4->4.4 we did not update the codebase to use Symfony Flex
You would do this with a Compiler Pass. (Docs are for 4.4, since that's the version you are using, current docs are here).
Change your application Kernel so it implements CompilerPassInterface, and on the process() method you can manipulate the service container directly:
// src/Kernel.php
namespace App;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
class Kernel extends BaseKernel implements CompilerPassInterface
{
use MicroKernelTrait;
// ...
public function process(ContainerBuilder $container): void
{
$container->getDefinition('monolog.logger')->setPublic(true);
}
}
In any case, if I were you I'd investigate using something like Rector to automatically update your code. It even includes a rule for this specific use-case, converting the (ab)use of injected container and public services into proper dependency injection.