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

查看:32
本文介绍了如何使用 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天全站免登陆