php - 如何配置 symfony 以记录弃用情况?

标签 php symfony deprecation-warning

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

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

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

 composer create-project symfony/website-skeleton:4.3.99

测试 Controller .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 仍然是空的。

最佳答案

这是我的弃用日志配置,使用 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 中发生的消息 channel (这是发送所有弃用日志消息的地方)。

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

关于php - 如何配置 symfony 以记录弃用情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59454913/

相关文章:

php - symfony/FOSRestBundle : empty JSON response (using the symfony embodied serializer)

php - Symfony2 FOSRest 将值设置为空

terraform - 不推荐使用 archive_file 作为资源

python - 在Python中生成STIX时的警告消息

php - 学说 2 实体关系删除

php - 仅在 mysql 中选择第一次出现的记录集组合

php - 像stackoverflow这样的积分系统

php - 获取异常代码列表

php - 是否可以使用 elasticsearch 提升 'newest' 项目? (FOQElasticaBundle)

python - 这是什么意思?弃用警告 : Call to deprecated function get_sheet_by_name (Use wb[sheetname])