Zend 启用 SQL 查询日志记录 [英] Zend Enable SQL Query logging

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

问题描述

我用它来检索数据库连接 atm.

I am using this to retrieve the database connection atm.

    $db = Zend_Db_Table::getDefaultAdapter();

我确实在我的配置中这样设置:

I do set this up in my config like this:

resources.db.adapter = pdo_mysql
resources.db.isDefaultTableAdapter = true
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = password
resources.db.params.dbname = db
resources.db.params.profiler.enabled = true
resources.db.params.profiler.class = Zend_Db_Profiler

例如,我想将所有内容输出到 sql.log.这可以应用于默认适配器吗?例如通过设置,那么我可以在生产环境中忽略它吗?

I would like to output everything to a sql.log for example. Is this possible to apply on the default adapter? for example through the settings, so I can ignore it in production environment?

很受用.

我确实看过:如何使用 Zend_Db 启用 SQL 输出到日志文件? 但它似乎没有涵盖我的问题.

I did look at: How to enable SQL output to log file with Zend_Db? but it didn't seem to cover my issue.

/马库斯

推荐答案

有一个扩展 Zend_Db_Profiler 的示例,以便您可以将查询写入/logs/db-queries.log 文件.

There is an example of extending Zend_Db_Profiler so you can write the queries to /logs/db-queries.log file.

所以你必须做到以下几点:

So you have to do the following:

  1. 在库文件夹中创建 My_Db_Profiler_Log 类
  2. 将以下行添加到 application.ini 中

resources.db.params.profiler.enabled = true

resources.db.params.profiler.enabled = true

resources.db.params.profiler.class = My_Db_Profiler_Log

resources.db.params.profiler.class = My_Db_Profiler_Log

注意:请注意,日志文件很快就会变得非常大!因此,只记录您感兴趣的查询是个好主意.本示例仅应被视为实现此类日志记录系统的起点.

Note: be aware, that the log file will become very big, very soon! So it is a good idea to log only the queries you are interested in. And this example should be considered only as a starting point in implementation of such a logging system.

这是自定义分析器类的代码:

Here is the code for the custom profiler class:

<?php

class My_Db_Profiler_Log extends Zend_Db_Profiler {

/**
 * Zend_Log instance
 * @var Zend_Log
 */
protected $_log;

/**
 * counter of the total elapsed time
 * @var double 
 */
protected $_totalElapsedTime;


public function __construct($enabled = false) {
    parent::__construct($enabled);

    $this->_log = new Zend_Log();
    $writer = new Zend_Log_Writer_Stream(APPLICATION_PATH . '/logs/db-queries.log');
    $this->_log->addWriter($writer);
}

/**
 * Intercept the query end and log the profiling data.
 *
 * @param  integer $queryId
 * @throws Zend_Db_Profiler_Exception
 * @return void
 */
public function queryEnd($queryId) {
    $state = parent::queryEnd($queryId);

    if (!$this->getEnabled() || $state == self::IGNORED) {
        return;
    }

    // get profile of the current query
    $profile = $this->getQueryProfile($queryId);



        // update totalElapsedTime counter
        $this->_totalElapsedTime += $profile->getElapsedSecs();

        // create the message to be logged
        $message = "\r\nElapsed Secs: " . round($profile->getElapsedSecs(), 5) . "\r\n";
        $message .= "Query: " . $profile->getQuery() . "\r\n";

        // log the message as INFO message
        $this->_log->info($message);

}

}

?>

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

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