如何在加载Doctrine灯具时禁用控制台中的查询日志记录? [英] How to disable query logging in console while load Doctrine fixtures?

查看:112
本文介绍了如何在加载Doctrine灯具时禁用控制台中的查询日志记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个装载大量数据的装置,一直遇到这个错误:

I have a fixtures that loads a huge amount of data and all the time I run into this error:


致命错误:允许
/var/www/html/platform-cm/vendor/doctrine/dbal/lib/Doctrine/DBAL/Logging/DebugStack.php中的内存大小为2147483648个字节(尝试
分配16777224个字节)
在第65行

Fatal error: Allowed memory size of 2147483648 bytes exhausted (tried to allocate 16777224 bytes) in /var/www/html/platform-cm/vendor/doctrine/dbal/lib/Doctrine/DBAL/Logging/DebugStack.php on line 65

[Symfony\Component\Debug\Exception\OutOfMemoryException]错误:
允许的内存大小为2147483648字节耗尽(尝试分配
16777224字节)

[Symfony\Component\Debug\Exception\OutOfMemoryException] Error: Allowed memory size of 2147483648 bytes exhausted (tried to allocate 16777224 bytes)

经过研究,我发现这个的帖子我读过这个日志记录可能是问题的原因,因为 AppKernel 被实例化,默认情况下debug设置为true,然后每次迭代将SQL命令存储在内存中。

After research a bit I found this post where I read that logging could be the cause of the issue because AppKernel is instantiated with debug set to true by default and then the SQL commands get stored in memory for each iteration.

AppKernel 中禁用调试的第一次尝试运行命令为:

The first attempt without disable the debug at AppKernel was run the command as:

doctrine:fixtures:load --no-debug

但是,由于同样的错误,我没有得到运气。

But I didn't get luck since the same error still.

第二次尝试是在 config_dev.yml 中禁用调试,但不建议,因为我正在乘坐每个日志都不能正常工作。

The second attempt was disable the debug at config_dev.yml but this is not recommended since I am getting ride of every logs but didn't work neither.

monolog:
    handlers:
        main:
            type:   stream
            path:   "%kernel.logs_dir%/%kernel.environment%.log"
            level:  debug
#        console:
#            type:   console
#            bubble: false
#            verbosity_levels:
#                VERBOSITY_VERBOSE: INFO
#                VERBOSITY_VERY_VERBOSE: DEBUG
#            channels: ["!doctrine"]
#        console_very_verbose:
#            type:   console
#            bubble: false
#            verbosity_levels:
#                VERBOSITY_VERBOSE: NOTICE
#                VERBOSITY_VERY_VERBOSE: NOTICE
#                VERBOSITY_DEBUG: DEBUG
#            channels: ["doctrine"]

所以,这是我的装置如何:

So, this is how my fixture looks like:

class LoadMsisdn extends AbstractFixture implements OrderedFixtureInterface
{
    public function getOrder()
    {
        return 13;
    }

    public function load(ObjectManager $manager)
    {
        $content = file_get_contents('number.txt');
        $numbers = explode(',', $content);
        shuffle($numbers);

        foreach ($numbers as $key => $number) {
            $msisdn = new Msisdn();
            $msisdn->setMsisdn($number);
            $msisdn->setBlocked((rand(1, 1000) % 10) < 7);
            $msisdn->setOperator($this->getReference('operator-' . rand(45, 47)));

            $this->addReference('msisdn-' . $key, $msisdn);
            $manager->persist($msisdn);
        }

        $manager->flush();
    }
}

如果我需要做,如何禁用记录器它来自 EntityManager ,如同一篇文章中的答案所示?

How do I disable the logger if I need to do it from EntityManager as shown in a answer on the same post?

$em->getConnection()->getConfiguration()->setSQLLogger(null);


推荐答案

正在传递给 load 方法是实体管理器的一个实例( Doctrine\Common\Persistence\ObjectManager 只是一个接口实体/文档/ etc管理器实现)。

The object manager that is being passed into the load method is an instance of the the entity manager (Doctrine\Common\Persistence\ObjectManager is just an interface that the entity/document/etc managers implement).

这意味着您可以使用与您的问题相同的命令来清除SQL记录器,如..

This mean that you can use the same command as in your question to nullify the SQL logger like..

$manager->getConnection()->getConfiguration()->setSQLLogger(null);

需要注意的一点是,DBAL连接的默认日志记录设置为 %kernel.debug%表示,除非您在配置中覆盖了它,否则只能在 dev 环境中进行日志记录。我可以看到你已经尝试使用 - no-debug 选项,但我只能假设,因为记录器设置为在容器编译期间,它不会将其设置为不被重建的容器。

One thing to note is that the default logging setting for a DBAL connection is %kernel.debug% meaning that, unless you have overridden it in your config, the logging should only happen in the dev environment. I can see you have tried using the --no-debug option but I can only assume that, as the logger is set during the container compilation, that it doesn't unset it to the container not being rebuilt.

这篇关于如何在加载Doctrine灯具时禁用控制台中的查询日志记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆