How to exclude deprecation messages from logs in Symfony 4?
Asked Answered
W

5

15

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?

Wagram answered 27/12, 2019 at 13:42 Comment(0)
B
19

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'
Belittle answered 27/12, 2019 at 13:49 Comment(4)
Hmm, I have tried this, but it's not working. It just keeps adding php.INFO: User Deprecated:Uranometry
This is a working example. I use it myself in many projects. Fix your config, clear the cache.Belittle
Thanks for responding. You are correct. There was a completely different issue at hand that prevented my changes from taking effect.Uranometry
This will exclude all levels of messages in the php channel. Ideally, it'd just exclude those of level INFO.Publicity
B
15

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]
Bobolink answered 1/11, 2022 at 20:33 Comment(0)
Z
4

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']
Zephyrus answered 26/12, 2022 at 8:36 Comment(0)
N
0

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

SYMFONY_DEPRECATIONS_HELPER=weak
Narcoanalysis answered 7/6, 2022 at 17:12 Comment(2)
Before saying it does not work (i was about to do it) search this env var in all the dot env filesMass
It "does not work" outside of the PHPUnit Bridge. Because that's the only package that evaluates that environment variable. Useless in my case, where a command emitted deprecation messages in prod environment. No tests involved at all.Disturbance
R
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.

Recourse answered 7/2, 2023 at 19:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.