Symfony 3.4 记录器服务 [英] Symfony 3.4 logger service

查看:33
本文介绍了Symfony 3.4 记录器服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用 logger 服务在日志文件中获取此信息消息时,它已工作但在日志文件中写入此消息:

When I called logger service get this information message in log file it's worked but write this message in the log file:

php.INFO:用户已弃用:记录器"服务是私有的,自 Symfony 以来不推荐从容器中获取它3.2 并将在 4.0 中失败.您应该将服务公开,或者直接停止使用容器并使用依赖注入反而.{"exception":"[object] (ErrorException(code: 0): User不推荐使用:\"logger\" 服务是私有的,从容器自 Symfony 3.2 起已被弃用,并将在 4.0 中失败.你应该公开服务,或者停止使用容器直接使用依赖注入代替.在/home/****/###/PROJECT/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:275)"}[]

php.INFO: User Deprecated: The "logger" service is private, getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0. You should either make the service public, or stop using the container directly and use dependency injection instead. {"exception":"[object] (ErrorException(code: 0): User Deprecated: The \"logger\" service is private, getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0. You should either make the service public, or stop using the container directly and use dependency injection instead. at /home/****/###/PROJECT/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:275)"} []

我的 symfony 版本:3.4.1

My symfony version: 3.4.1

推荐答案

如 Symfony 3.4 所述,MonologBu​​ndle 和所有其他服务提供的 logger 服务,是默认设置为私有.[原文如此]

As stated in Symfony 3.4, the logger service provided by the MonologBundle and all other services, are set to private by default. [sic]

要解决此问题,推荐的方法是使用依赖注入.http://symfony.com/doc/3.4/logging.html

To workaround the issue, the recommended method is to use Dependency Injection. http://symfony.com/doc/3.4/logging.html

namespace AppBundle\Controller;

use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
     public function indexAction(LoggerInterface $logger)
     {
        $logger->info('Your Message');
     }
}

源代码参考:https://github.com/symfony/monolog-bundle/blob/v3.1.0/Resources/config/monolog.xml#L17

对于服务定义依赖注入在 autowire 启用时可用.[原文如此]

For service definitions Dependency Injection is available when autowire is enabled. [sic]

#app/config/services.yml

services:
    # default configuration for services in *this* file
    _defaults:
        # automatically injects dependencies in your services
        autowire: true
        # automatically registers your services as commands, event subscribers, etc.
        autoconfigure: true
        # this means you cannot fetch services directly from the container via $container->get()
        # if you need to do this, you can override this setting on individual services
        public: false

    # makes classes in src/AppBundle available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    AppBundle\:
        resource: '../../src/AppBundle/*'
        # you can exclude directories or files
        # but if a service is unused, it's removed anyway
        exclude: '../../src/AppBundle/{Entity,Repository,Tests}'

    #enables dependency injection in controller actions
    AppBundle\Controller\:
        resource: '../../src/AppBundle/Controller'
        public: true
        tags: ['controller.service_arguments']

   #all of your custom services should be below this line
   #which will override the above configurations

    #optionally declare an individual service as public
    #AppBundle\Service\MyService: 
    #    public: true

    #alternatively declare the namespace explicitly as public
    #AppBundle\Service\:
    #    resource: '../../src/AppBundle/Service/*'
    #    public: true

然后为了将依赖项注入服务,您将参数的类型提示添加到构造函数中.

Then to Inject the Dependency into the service, you add the type hint for the argument to the constructor.

namespace AppBundle\Service;

use Psr\Log\LoggerInterface;

class MyService
{

    private $logger;
    
    public function __construct(LoggerInterface $logger)
    {
         $this->logger = $logger;
    }
   
}

如果 autowire 被禁用,您可以手动定义您的服务以注入记录器别名.

if autowire is disabled, you can manually define your services to inject the logger alias.

#app/config/services.yml

services:

    AppBundle\Service\MyService:
        arguments: ['@logger']
        public: true


或者,要强制从容器公开访问记录器别名,您可以在应用程序服务配置中重新声明服务别名.


Alternatively, to force the logger alias to be publicly accessible from the container, you can re-declare the service alias in your application services config.

#app/config/services.yml

services:

    #...
    
    logger:
        alias: 'monolog.logger'
        public: true


除了覆盖配置中的值,您还可以在编译器传递中将记录器设置为公共服务.https://symfony.com/doc/4.4/service_container/compiler_passes.html

Symfony Flex

// 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)
     {
        // in this method you can manipulate the service container:
        // for example, changing some container service:
        $container->getDefinition('logger')->setPublic(true);
    }

}

Symfony 捆绑包

// src/AppBundle/AppBundle.php
namespace AppBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use AppBundle\DependencyInjection\Compiler\CustomPass;

class AppBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
        parent::build($container);

        $container->addCompilerPass(new CustomPass());
    }
}

// src/AppBundle/DependencyInjection/Compiler/CustomPass.php
namespace AppBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class CustomPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $container->getDefinition('logger')->setPublic(true);
    }
}

这篇关于Symfony 3.4 记录器服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆