4

I'd like to upgrade from Symfony 4.4. to 5.0. So I have to check for deprecations in the code. The symfony migration guide says I have to use the web dev toolbar, but in my API-app there is no frontend for the tool-bar.

How can I configure symfony/monolog to log deprecation warnings to the log file?

Update I have created a minimal example:

 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'
        ]);
    }
}

monolog.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"]

run server

 bin/console server:run

open http://localhost/test

But the dev.deprecations.log is still empty.

yivi
  • 42,438
  • 18
  • 116
  • 138
oceanBT
  • 204
  • 6
  • 11
  • It seems that the debug toolbar in dev mode captures these deprecation notices, because they don't appear in the standard log file. I suspect a compiler pass is intercepting them. – PeterB Feb 06 '20 at 09:11
  • I created a bug report / question on github https://github.com/symfony/symfony/issues/35367 – oceanBT Feb 19 '20 at 09:25

2 Answers2

4

This is my deprecation log configuration, using 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"]

The deprecation_stream specify a log file to log these messages.

The deprecation_filter specifies which messages should be logged: info messages happening in the php channel (this is where all deprecation log messages are sent).

You can enable this application wide, or only set it up this way in whatever environment you want to catch these messages.

yivi
  • 42,438
  • 18
  • 116
  • 138
  • Sorry, doesn't work for me. I have installed the dev-toolbar and created a demo controller. There I see the deprecation warnings, but no where else. I don't get it why the dev-toolbar can catch these warnings. I try to create a minimal example. – oceanBT Jan 02 '20 at 18:58
  • i have updated the question with an minimal example. – oceanBT Jan 14 '20 at 13:30
0

There was a bug in Symfony 4.4 which is now fixed. More details here.

oceanBT
  • 204
  • 6
  • 11