我已经将应用程序从 Symfony 3.4 迁移到 Symfony 4.4。
现在我对每个请求/Sf 命令都有很多弃用(我无法修复这些弃用)。
如何从这个 Symfony 应用程序的日志中排除弃用?
从日志处理程序中排除
php
通道:
例如。
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
在
dev
模式下保留弃用消息。你应该知道上游包的变化。
附言。在较新的 Symfony 版本中,您必须改为排除“弃用”渠道。
在 Symfony 6.1.X 中对我有用的是将它设置为
deprecation
频道并输入 "null"
。这将确保弃用消息不会显示在日志记录中但是仍然可以从调试工具栏中看到。
# config/packages/dev/monolog.yaml
monolog:
channels:
- deprecation
handlers:
deprecation:
type: "null"
channels: [deprecation]
设置以下环境变量(例如在 .env.local 中):
SYMFONY_DEPRECATIONS_HELPER=weak
为我工作Symfony 5.4+
添加频道列表 'deprecation',并在处理参数 '!deprecation' 中使用
它的工作过滤器示例:
monolog:
channels:
- 'deprecation'
handlers:
main:
type: rotating_file
max_files: 30
path: '%kernel.logs_dir%/%kernel.environment%.log'
level: debug
channels: ['!event', '!doctrine', '!deprecation']
Symfony\Component\HttpKernel\EventListener\DebugHandlersListener
控制 应记录的错误级别由构造函数参数 $levels = \E_ALL
定义,默认情况下为 E_ALL
。不幸的是,该值在服务定义中是静态的。但是你可以通过编译器传递来覆盖它并引入一个参数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')
);
}
}
此编译器通行证已在您的包中注册:
<?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());
}
}
在您的参数文件中,您可以将值设置为
E_ALL & ~ E_DEPRECATED & ~ E_USER_DEPRECATED
,即8191
:
php_log_level: 8191
请注意,这适用于 Symfony 3.4 和 4.4。