如何使用CakePHP将SQL查询记录到日志文件 [英] How to log SQL queries to a log file with CakePHP

查看:319
本文介绍了如何使用CakePHP将SQL查询记录到日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CakePHP 1.2应用程序,使用AjaxHelper对象进行大量的AJAX调用。 AjaxHelper调用控制器函数,然后将一些数据返回到页面。

I have a CakePHP 1.2 application that makes a number of AJAX calls using the AjaxHelper object. The AjaxHelper makes a call to a controller function which then returns some data back to the page.

我想记录由AJAX控制器函数执行的SQL查询。通常,我只是在config / core.php中调试级别为2,但是,这打破了我的AJAX功能,因为它导致输出SQL查询附加到返回到客户端的输出。

I would like to log the SQL queries that are executed by the AJAX controller functions. Normally, I would just turn the debug level to 2 in config/core.php, however, this breaks my AJAX functionality because it causes the output SQL queries to be appended to the output that is returned to the client side.

为了解决这个问题,我想能够将执行的任何SQL查询记录到日志文件。任何建议?

To get around this issue, I would like to be able to log any SQL queries performed to a log file. Any suggestions?

推荐答案

我在这个链接找到了一个很好的添加日志功能的方法:

I found a nice way of adding this logging functionality at this link:

http://cakephp.1045679。 n5.nabble.com/Log-SQL-queries-td1281970.html

基本上,在 cake / libs / model / datasources / dbo / 目录中,您可以使用您正在使用的dbo的子类。例如,如果您使用dbo_mysql.php数据库驱动程序,则可以创建一个名为 dbo_mysql_with_log.php 的新类文件。该文件将包含一些代码如下:

Basically, in your cake/libs/model/datasources/dbo/ directory, you can make a subclass of the dbo that you're using. For example, if you're using the dbo_mysql.php database driver, then you can make a new class file called dbo_mysql_with_log.php. The file would contain some code along the lines of the following:

App::import('Core', array('Model', 'datasource', 'dbosource', 'dbomysql'));

class DboMysqlWithLog extends DboMysql {
    function _execute($sql) {
        $this->log($sql);
        return parent::_execute($sql);
    }
}

简而言之,此类修改(即覆盖) _execute 函数用于在执行任何通常执行的逻辑之前记录SQL查询。

In a nutshell, this class modifies (i.e. overrides) the _execute function of the superclass to log the SQL query before doing whatever logic it normally does.

您的 app / config / database.php 配置文件以使用您刚创建的新驱动程序。

You can modify your app/config/database.php configuration file to use the new driver that you just created.

这篇关于如何使用CakePHP将SQL查询记录到日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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