【问题标题】:How to config symfony to log deprecations?如何配置 symfony 来记录弃用?
【发布时间】:2020-04-14 17:14:24
【问题描述】:

我想从 Symfony 4.4 升级。到 5.0。所以我必须检查代码中的弃用。 symfony migration guide 说我必须使用 web 开发工具栏,但是在我的 API 应用程序中没有工具栏的前端。

如何配置 symfony/monolog 以将弃用警告记录到日志文件中?

更新 我创建了一个最小的示例:

 composer create-project symfony/website-skeleton:4.3.99

TestController.php

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * Class ApiController
 * @package App\Controller
 * @Route("", defaults={"_format"="json"})
 *
 */
class TestController extends AbstractController
{

    /**
     * @Route("/test", name="test")
     * @param Request $request
     * @return Response
     */
    public function test(Request $request): Response
    {
        @trigger_error(sprintf('DEMO DEPRECATION', __METHOD__), E_USER_DEPRECATED);
        return $this->json([
            'test' => '1'
        ]);
    }
}

独白.yml

monolog:
    handlers:
        main:
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
            channels: ["!event"]
        # uncomment to get logging in your browser
        # you may have to allow bigger header sizes in your Web server configuration
        #firephp:
        #    type: firephp
        #    level: info
        #chromephp:
        #    type: chromephp
        #    level: info
        console:
            type: console
            process_psr_3_messages: false
            channels: ["!event", "!doctrine", "!console"]
        deprecation_stream:
              type: stream
              path: "%kernel.logs_dir%/%kernel.environment%.deprecations.log"
        deprecation_filter:
             type: filter
             handler: deprecation_stream
             max_level: info
             channels: ["php"]

运行服务器

 bin/console server:run

打开http://localhost/test

但是 dev.deprecations.log 仍然是空的。

【问题讨论】:

  • 开发模式下的调试工具栏似乎捕获了这些弃用通知,因为它们没有出现在标准日志文件中。我怀疑编译器传递正在拦截它们。
  • 我在github上创建了一个错误报告/问题github.com/symfony/symfony/issues/35367

标签: php symfony monolog deprecation-warning


【解决方案1】:

这是我的弃用日志配置,使用 Monolog:

monolog:
  handlers:
  # other handlers...

    ### Deprecation logs
    deprecation_stream:
      type: stream
      path: "%kernel.logs_dir%/%kernel.environment%.deprecations.log"

    deprecation_filter:
      type: filter
      handler: deprecation_stream
      max_level: info
      channels: ["php"]

deprecation_stream 指定一个日志文件来记录这些消息。

deprecation_filter 指定应记录哪些消息:info 消息发生在 php 通道中(这是发送所有弃用日志消息的地方)。

您可以在整个应用程序范围内启用此应用程序,或者仅在您想要捕获这些消息的任何环境中以这种方式设置它。

【讨论】:

  • 对不起,对我不起作用。我已经安装了开发工具栏并创建了一个演示控制器。在那里我看到了弃用警告,但没有其他地方。我不明白为什么开发工具栏可以捕获这些警告。我尝试创建一个最小的示例。
  • 我已经用一个最小的例子更新了这个问题。
【解决方案2】:

Symfony 4.4 中存在一个错误,现已修复。 More details here.

【讨论】:

    猜你喜欢
    • 2020-04-21
    • 2023-03-19
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 2018-07-01
    • 2019-07-26
    • 1970-01-01
    相关资源
    最近更新 更多