12

I have migrated an application from Symfony 3.4 to Symfony 4.4.

Now I have a lot of deprecations for each request/ Sf command (I can't fix that deprecations).

How can I exclude deprecations from the log for this Symfony App?

Mike Doe
  • 16,349
  • 11
  • 65
  • 88
acantepie
  • 329
  • 1
  • 3
  • 9

5 Answers5

15

Exclude the php channel from the log handler:

Eg. config/packages/prod/monolog.yaml:

monolog:
    handlers:
        main:
            type:  stream
            path:  %kernel.logs_dir%/%kernel.environment%.log
            level: debug
            formatter: monolog.formatter.session_request
            channels:
             -  '!php' # <----------- add this line

Leave deprecation messages in the dev mode though. You should be aware of the changes in upstream packages.

PS. in newer Symfony versions you have to exclude the deprecation channel instead, i.e.:

channels:
    - '!deprecation'
Mike Doe
  • 16,349
  • 11
  • 65
  • 88
  • 3
    Hmm, I have tried this, but it's not working. It just keeps adding `php.INFO: User Deprecated:` – Dion Snoeijen Mar 31 '20 at 11:43
  • 1
    This is a working example. I use it myself in many projects. Fix your config, clear the cache. – Mike Doe Mar 31 '20 at 11:44
  • Thanks for responding. You are correct. There was a completely different issue at hand that prevented my changes from taking effect. – Dion Snoeijen Mar 31 '20 at 14:50
  • 2
    This will exclude _all_ levels of messages in the php channel. Ideally, it'd just exclude those of level INFO. – Andy Dec 10 '21 at 10:13
8

What works for me in Symfony 6.1.X, is setting it the deprecation channel and type to "null". This will make sure the deprecation messages will not show up in the logging but still be available to see from the debug toolbar.

# config/packages/dev/monolog.yaml
monolog:
  channels:
    - deprecation
  handlers:
    deprecation:
      type: "null"
      channels: [deprecation]
YTZ
  • 876
  • 11
  • 26
1

Set the following env variable (eg. in .env.local):

SYMFONY_DEPRECATIONS_HELPER=weak
Micronax
  • 660
  • 13
  • 25
1

Work for me Symfony 5.4+

add in channels list 'deprecation', and use in handles argument '!deprecation'

its work filter example:

monolog:
    channels:
        - 'deprecation'
    handlers:
        main:
            type: rotating_file
            max_files: 30
            path: '%kernel.logs_dir%/%kernel.environment%.log'
            level: debug
            channels: ['!event', '!doctrine', '!deprecation']
XNicON
  • 153
  • 1
  • 6
0

The error handlers are controlled by Symfony\Component\HttpKernel\EventListener\DebugHandlersListener Error levels that should be logged are defined by the constructor argument $levels = \E_ALL which is by default E_ALL. Unfortunately the value is static in the service definition. But you can override it by a compiler pass and introduce a parameter php_log_level:

<?php
declare(strict_types=1);

namespace YourBundle\DependencyInjection;

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

class PhpErrorLevelCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        if(! $container->hasDefinition('debug.debug_handlers_listener') || ! $container->hasParameter('php_log_level')) {
            return;
        }
        $container
            ->findDefinition('debug.debug_handlers_listener')
            ->replaceArgument(
                2,
                (int) $container->getParameter('php_log_level')
            );
    }
}

This compiler pass is registered in your bundle:

<?php
declare(strict_types=1);

namespace YourBundle;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use YourBundle\DependencyInjection\PhpErrorLevelCompilerPass;

class YourBundle extends Bundle {

    public function build(ContainerBuilder $container)
    {
        parent::build($container);
        $container->addCompilerPass(new PhpErrorLevelCompilerPass());
    }
}

In your parameters file you can then set the value to E_ALL & ~ E_DEPRECATED & ~ E_USER_DEPRECATED which is 8191:

php_log_level: 8191

Note that this applies for Symfony 3.4 and 4.4.

David
  • 803
  • 6
  • 13