使用自定义 Monolog 通道升级 Symfony 3 项目

问题描述

我正在升级一个 Symfony 项目,目前是 SF3.4、PHP 7.1、monolog 包版本 3.1。 我想摆脱弃用消息,以便能够首先升级到 SF 4,然后升级到 SF5。 我遇到了一个 monolog 问题:除了常规频道之外,还有一个自定义频道可以在 cutom 文件中写入日志,在此代码中用于所​​有命令,在其他文件中还有另一个频道用于其他类型的操作。

我的问题是,我不知道如何编辑配置以保持这种行为。

我已关注 https://symfony.com/doc/3.4/logging/channels_handlers.htmlhttps://symfony.com/doc/3.4/service_container.html#services-wire-specific-service

但是当我调用该命令时,日志仍然写入文件(dev模式下的dev.log)

目前,我有 在主 config.yml

monolog:
    channels: ["command"]
    handlers:
        command:   # Custom handler
            type: stream
            path:  "%kernel.logs_dir%/command_%kernel.environment%.log"   # Custom log file
            channels: [command]   # Custom channel

在命令类中

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Psr\Log\LoggerInterface;

class AlertOverdueCommand extends ContainerAwareCommand
{
  private $logger;

  public function __construct(LoggerInterface $logger)
  {
      $this->logger = $logger;
      //other stuff
      parent::__construct();
  }

  protected function execute(InputInterface $input,OutputInterface $output)
  {
    //Some other code
    
    //Log action
    $info = 'A cutom message';
    /**
     * I replaced,to get rid of the deprecation : 
     * $this->getContainer()->get('monolog.logger.command')->info($info);
     * => It logged in var/log/command.log
     */
    $this->logger->info($info);
    /**
     * => This logs in var/log/dev.log (in dev mode)
     */
  }
}

并在服务配置中(用于命令的 command.yml)

services:
    _defaults:
        autowire: true
        autoconfigure: true
        tags:
            - { name: monolog.logger,channel: command }

    project.alertloanoverdue.command:
        class: The\Nampespace\Of\The\Class
        autoconfigure: false
        arguments: ["@monolog.logger.command"]
        public: true

我检查了这个服务是否存在 PHP bin/console debug:container monolog.logger.command

information for Service "monolog.logger.command"
================================================

 ---------------- ------------------------------------------------------ 
  Option           Value                                                 
 ---------------- ------------------------------------------------------ 
  Service ID       monolog.logger.command                                
  Class            Symfony\Bridge\Monolog\Logger                         
  Tags             -                                                     
  Calls            pushProcessor,pushHandler,pushHandler  
  Public           yes                                                   
  Synthetic        no                                                    
  Lazy             no                                                    
  Shared           yes                                                   
  Abstract         no                                                    
  Autowired        no                                                    
  Autoconfigured   no 

我后来也在 composer.json 中将 Monolog 包升级到 3.5 以便能够自动连接记录器通道 https://symfony.com/doc/current/logging/channels_handlers.html#how-to-autowire-logger-channels 但它似乎也不起作用,(也许这是正常的:此信息未出现在 SF 3.4 版本的文档中)

{
    "require": {
        "PHP": ">=7.1.0",...
        "symfony/monolog-bundle": "^3.5.0",}
}

然后我在命令类里改了

public function __construct(LoggerInterface $commandLogger)

编辑:显然,此功能is only available after SF4.2,所以我想这不是一个选项。

在此先感谢您的帮助

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...