加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

zend-framework – Zend启用SQL查询日志记录

发布时间:2020-12-13 17:51:34 所属栏目:PHP教程 来源:网络整理
导读:我用它来检索数据库连接atm. $db = Zend_Db_Table::getDefaultAdapter(); 我在我的配置中设置了这样: resources.db.adapter = pdo_mysqlresources.db.isDefaultTableAdapter = trueresources.db.params.host = localhostresources.db.params.username = roo
我用它来检索数据库连接atm.
$db = Zend_Db_Table::getDefaultAdapter();

我在我的配置中设置了这样:

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中.这可以应用于默认适配器吗?例如通过设置,所以我可以在生产环境中忽略它?

非常感兴趣.

我确实看过:How to enable SQL output to log file with Zend_Db?,但它似乎没有涵盖我的问题.

/马库斯

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

所以你必须做以下事情:

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

resources.db.params.profiler.enabled = true

resources.db.params.profiler.class = My_Db_Profiler_Log

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

以下是自定义探查器类的代码:

<?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 = "rnElapsed Secs: " . round($profile->getElapsedSecs(),5) . "rn";
        $message .= "Query: " . $profile->getQuery() . "rn";

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

}

}

?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读