get('logger') 的引用。来自各地。 我一直在尝试公开这项服务,但我找不到任何例子。 错误:-6ren">
gpt4 book ai didi

php - 如何更改 Symfony 上 "logger"服务的可见性?

转载 作者:行者123 更新时间:2023-12-04 12:21:54 24 4
gpt4 key购买 nike

我最近从 Symfony 3.4 升级到 4.4
代码库真的很大,有很多对 container->get('logger') 的引用。来自各地。
我一直在尝试公开这项服务,但我找不到任何例子。
错误:

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服务大众?
注意:在从 3.4->4.4 的升级中,我们没有更新代码库以使用 Symfony Flex

最佳答案

你可以用 Compiler Pass 来做到这一点。 . (文档适用于 4.4,因为这是您使用的版本,当前文档是 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 的东西进行调查。自动更新您的代码。它甚至包括 a rule for this specific use-case ,将注入(inject)的容器和公共(public)服务的(ab)使用转换为适当的依赖注入(inject)。

关于php - 如何更改 Symfony 上 "logger"服务的可见性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68449631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com