如何阻止 Symfony 记录 Doctrine 的 sql 查询?

How to stop Symfony from logging Doctrine's sql queries?

我有一个奇怪的问题,当我检查我的 app/log/dev.log 时,我可以看到我 dev.log 中的几乎所有查询都被实时记录:

[2015-01-27 06:57:22] doctrine.DEBUG: SELECT t0.username A ....
[2015-01-27 06:57:23] doctrine.DEBUG: SELECT t0.username A ...
[2015-01-27 06:57:23] doctrine.DEBUG: SELECT s0_.id ......

我不知道为什么会这样,因为我 运行 当我在我的 config.yml 中检查 monolog 时,网站也处于生产模式,这是我看到的:

monolog:
    handlers:
        pictures:
            type: stream
            path: %kernel.logs_dir%/pictures_%kernel.environment%.log
            level: info
        instagram:
            type: stream
            path: %kernel.logs_dir%/instagram_%kernel.environment%.log
            level: info

这是我的 config_dev.yml 的样子:

imports:
    - { resource: config.yml }

framework:
    router:   { resource: "%kernel.root_dir%/config/routing_dev.yml" }
    profiler: { only_exceptions: false }

web_profiler:
    toolbar: true
    intercept_redirects: false

monolog:
    handlers:
        main:
            type:  stream
            path:  %kernel.logs_dir%/%kernel.environment%.log
            level: debug
        firephp:
            type:  firephp
            level: info

assetic:
    use_controller: false

hip_mandrill:
    disable_delivery: true    

知道这是怎么发生的吗?

您应该在生产服务器上使用 prod env。在 prod 环境原则中,默认情况下禁用日志记录。

但是如果你想完全禁用日志记录(在所有环境中),你需要像这样设置 config.yml

doctrine:
    dbal:
        connections:
            conn1:
                driver: ...
                ...
                logging: false
                profiling: false

参考:https://symfony.com/doc/current/bundles/DoctrineBundle/configuration.html

我在 prod 环境中生成 dev.log 时遇到了类似的问题。我从日志条目中发现导致我的问题的原因是计划的 cron 作业调用自定义 symfony 命令。自从停止生成 dev.log 以来,将我的 crontab 的条目修改为 app/console--env=prod。即

app/console --env=prod custom:command 

一定是错过了这本书的那一部分:)