Symfony3+PHP单元。如何禁用控制台中的调试日志?

Symfony3+PHPUnit. How to disable debug log in console?

我想我在所有地方都关闭了调试日志记录(在 config.yml、config_dev、config_test):

monolog:
handlers:
    main:
        type: stream
        path: "%kernel.logs_dir%/%kernel.environment%.log"
        level: debug
        channels: [!event]
    console:
        type: stream
        path: "%kernel.logs_dir%/console.log"
        level: critical
        channels: [!event]

但是当我 运行 phpunit 时它显示调试日志:

[2016-06-19 16:53:29] event.DEBUG: Notified event "{event}" to listener "{listener}". {"event":"kernel.request","listener":"Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure"} []
[2016-06-19 16:53:29] event.DEBUG: Notified event "{event}" to listener "{listener}". {"event":"kernel.request","listener":"Symfony\Component\HttpKernel\EventListener\DumpListener::configure"} []
[2016-06-19 16:53:29] event.DEBUG: Notified event "{event}" to listener "{listener}". {"event":"kernel.request","listener":"Symfony\Bundle\FrameworkBundle\EventListener\TestSessionListener::onKernelRequest"} []
[2016-06-19 16:53:29] event.DEBUG: Notified event "{event}" to listener "{listener}". {"event":"kernel.request","listener":"Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest"} []
[2016-06-19 16:53:29] event.DEBUG: Notified event "{event}" to listener "{listener}". {"event":"kernel.request","listener":"Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest"} []
[2016-06-19 16:53:29] event.DEBUG: Notified event "{event}" to listener "{listener}". {"event":"kernel.request","listener":"Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest"} []
[2016-06-19 16:53:29] event.DEBUG: Notified event "{event}" to listener "{listener}". {"event":"kernel.request","listener":"Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest"} []
[2016-06-19 16:53:29] event.DEBUG: Notified event "{event}" to listener "{listener}". {"event":"kernel.request","listener":"AppBundle\EventListener\LocaleListener::onKernelRequest"} []

如何禁用它?

测试环境似乎 运行 处于 app_dev.php 中应用程序内核 $kernel = new AppKernel('dev', true); 中启用的调试模式。生产环境也是如此,但有一个例外:第二个参数应该是 false,这意味着调试模式被禁用。

要消除测试环境中的调试输出,您需要将 --no-debug 标志传递给您的命令,例如: php bin/console some:command --env=test --no-debug

对于 PHPUnit,它将不起作用。您可能需要为 dev 环境覆盖您的应用程序内核:

示例来自 Kris Wallsmith 博客:

class AppKernel extends Kernel
{
    // ...

    protected function initializeContainer()
    {
        static $first = true;

        if ('test' !== $this->getEnvironment()) {
            parent::initializeContainer();
            return;
        }

        $debug = $this->debug;

        if (!$first) {
            // disable debug mode on all but the first initialization
            $this->debug = false;
        }

        // will not work with --process-isolation
        $first = false;

        try {
            parent::initializeContainer();
        } catch (\Exception $e) {
            $this->debug = $debug;
            throw $e;
        }

        $this->debug = $debug;
    }
}

如果你试试这个:

  console:
     type: stream
     path: "%kernel.logs_dir%/console.log"
     level: critical
     channels: []

那应该没问题。我对此也有一些问题,我只是根据我的需要对其进行了评论。 如果你还想对 doctrine 进行一些调试,只需将 !doctrine 放在括号中即可。 如果我没猜错,那就是你需要的。