如何更改 "logger" 服务在 Symfony 上的可见性?
How to change the visibility for the "logger" service on Symfony?
我最近从 Symfony 3.4 升级到 4.4
代码库非常大,到处都有很多对 container->get('logger')
的引用。
我一直在尝试提供这项服务 public,但我找不到任何示例。
错误:
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.
在我的 service.yml
中,我已经试过了,但没有成功:
Psr\Log\LoggerInterface:
public: true
Psr\Log\LoggerInterface:
alias: 'logger'
public: true
Psr\Log\LoggerInterface:
alias: 'monolog.logger'
public: true
如何制作 logger
服务 public?
注意:在从 3.4->4.4 的升级中,我们没有更新代码库以使用 Symfony Flex
你可以使用 Compiler Pass. (Docs are for 4.4, since that's the version you are using, current docs are here).
更改您的应用程序 Kernel
使其实现 CompilerPassInterface
,并且在 process()
方法上您可以直接操作服务容器:
// 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);
}
}
无论如何,如果我是你,我会调查使用类似 Rector to automatically update your code. It even includes a rule for this specific use-case 的方法,将注入容器和 public 服务的(滥用)使用转换为适当的依赖注入。
我最近从 Symfony 3.4 升级到 4.4
代码库非常大,到处都有很多对 container->get('logger')
的引用。
我一直在尝试提供这项服务 public,但我找不到任何示例。 错误:
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.
在我的 service.yml
中,我已经试过了,但没有成功:
Psr\Log\LoggerInterface:
public: true
Psr\Log\LoggerInterface:
alias: 'logger'
public: true
Psr\Log\LoggerInterface:
alias: 'monolog.logger'
public: true
如何制作 logger
服务 public?
注意:在从 3.4->4.4 的升级中,我们没有更新代码库以使用 Symfony Flex
你可以使用 Compiler Pass. (Docs are for 4.4, since that's the version you are using, current docs are here).
更改您的应用程序 Kernel
使其实现 CompilerPassInterface
,并且在 process()
方法上您可以直接操作服务容器:
// 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);
}
}
无论如何,如果我是你,我会调查使用类似 Rector to automatically update your code. It even includes a rule for this specific use-case 的方法,将注入容器和 public 服务的(滥用)使用转换为适当的依赖注入。