php 如何让 Doctrine 在 Symfony2 中记录查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29470738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:25:46  来源:igfitidea点击:

How to get Doctrine to log queries in Symfony2

phpsymfonydoctrine-ormmonolog

提问by Andy Raines

I'm pretty new to Symfony2, and I'm looking for a way to log SQL queries (including timings) to the same log file as the rest of my application.

我是 Symfony2 的新手,我正在寻找一种方法将 SQL 查询(包括计时)记录到与我的应用程序的其余部分相同的日志文件中。

From what I can determine from the documentation this should all work out of the box, but after a few hours of trying I can't figure out what I'm doing wrong.

根据我从文档中可以确定的内容,这应该都是开箱即用的,但是经过几个小时的尝试后,我无法弄清楚我做错了什么。

config_dev.yml

config_dev.yml

monolog:
    handlers:
        doctrine:
            action_level: debug
            type: stream
            path: %kernel.logs_dir%/%kernel.environment%_doctrine.log
            channels: doctrine

config.yml

配置文件

# Doctrine Configuration
doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        logging:  true
        profiling:  true

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true

I get no log file generated at all. My other logging handler works fine (not listed here).

我根本没有生成日志文件。我的其他日志处理程序工作正常(此处未列出)。

I'm wondering where I've gone wrong here, but also whether this is really the right approach or if I should implement a new class which implements the SQL Logger, as mentioned here: http://vvv.tobiassjosten.net/symfony/logging-doctrine-queries-in-symfony2/

我想知道我在这里出了什么问题,但也想知道这是否真的是正确的方法,或者我是否应该实现一个实现 SQL Logger 的新类,如下所述:http: //vvv.tobiassjosten.net/symfony /logging-doctrine-queries-in-symfony2/

But I've no idea how to plug that in via the configuration/services in order to make it apply project-wide (I don't want to have to call it in every Controller, for example).

但是我不知道如何通过配置/服务插入它以使其在项目范围内应用(例如,我不想在每个控制器中调用它)。

Many thanks for any help!

非常感谢您的帮助!

回答by Stev

If you are really sure that you need to log doctrine 2 queries in production then you can set this up in the configs for doctrine.

如果您真的确定需要在生产中记录学说 2 查询,那么您可以在学说的配置中进行设置。

connections:
        # A collection of different named connections (e.g. default, conn2, etc)
        default:
                # when true, queries are logged to a "doctrine" monolog channel
            logging: true 

http://symfony.com/doc/current/reference/configuration/doctrine.html

http://symfony.com/doc/current/reference/configuration/doctrine.html

And config monolog to log doctrine like explained in the docs: http://symfony.com/doc/current/cookbook/logging/channels_handlers.html

并配置 monolog 以记录教义,如文档中所述:http: //symfony.com/doc/current/cookbook/logging/channels_handlers.html

A similar issue can be found at symfony 2.4 can't get the doctrine channel in prod environment

symfony 2.4 can't get the police channel in prod environment可以找到类似的问题