如何使用 CakePHP 将 SQL 查询记录到日志文件
我有一个 CakePHP 1.2 应用程序,它使用 AjaxHelper 对象进行多次 AJAX 调用。 AjaxHelper 调用控制器函数,然后将一些数据返回到页面。
我想记录 AJAX 控制器函数执行的 SQL 查询。通常,我会在 config/core.php 中将调试级别设置为 2,但是,这会破坏我的 AJAX 功能,因为它会导致输出 SQL 查询附加到返回到客户端的输出中。
为了解决这个问题,我希望能够将执行的任何 SQL 查询记录到日志文件中。有什么建议吗?
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.
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.
To get around this issue, I would like to be able to log any SQL queries performed to a log file. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在此链接中找到了添加此日志记录功能的好方法:
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
的新类文件。该文件将包含一些类似于以下内容的代码:简而言之,此类修改(即覆盖)超类的
_execute
函数,以在执行其通常执行的任何逻辑之前记录 SQL 查询。您可以修改
app/config/database.php
配置文件以使用刚刚创建的新驱动程序。I found a nice way of adding this logging functionality at this link:
http://cakephp.1045679.n5.nabble.com/Log-SQL-queries-td1281970.html
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 calleddbo_mysql_with_log.php
. The file would contain some code along the lines of the following: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.You can modify your
app/config/database.php
configuration file to use the new driver that you just created.这是调试此类内容的绝佳方法,https://github.com/cakephp/debug_kit
This is a fantastic way to debug things like this, https://github.com/cakephp/debug_kit