2011-11-13 112 views
6

我正在使用它来检索数据库连接atm。Zend启用SQL查询日志记录

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

很多appriciated。

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

/马库斯

回答

2

延长Zend_Db_Profiler写的SQL.log和分析器连接到你的数据库适配器

<?php 

class File_Profiler extends Zend_Db_Profiler { 
/** 
    * The filename to save the queries 
    * 
    * @var string 
    */ 
protected $_filename; 

/** 
    * The file handle 
    * 
    * @var resource 
    */ 
    protected $_handle = null; 

/** 
    * Class constructor 
    * 
    * @param string $filename 
    */ 
public function __construct($filename) { 
    $this->_filename = $filename; 
} 

/** 
    * Change the profiler status. If the profiler is not enabled no 
    * query will be written to the destination file 
    * 
    * @param boolean $enabled 
    */ 
public function setEnabled($enabled) { 
    parent::setEnabled($enabled); 

    if($this->getEnabled()) { 
    if(!$this->_handle) { 
     if(!($this->_handle = @fopen($this->_filename, "a"))) { 
     throw new Exception("Unable to open filename {$this->_filename} for query profiling"); 
     } 
    } 
    } 
    else { 
    if($this->_handle) { 
     @fclose($this->_handle); 
    } 
    } 
} 

/** 
    * Intercept parent::queryEnd to catch the query and write it to a file 
    * 
    * @param int $queryId 
    */ 
public function queryEnd($queryId) { 
    $state = parent::queryEnd($queryId); 

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

    $profile = $this->getQueryProfile($queryId); 

    @fwrite($this->_handle, round($profile->getElapsedSecs(),5) . " " . $profile->getQuery() . " " . ($params=$profile->getQueryParams())?$params:null); 
} 
} 

还没有测试,但它应该做的伎俩。试试看,让我知道。

顺便说一句,你知道你可以在MySQL上记录所有查询吗?

+0

你能探索一点好吗? – Oldek

+0

太棒了,但是请显示这个的使用样品。 – Vlado

12

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

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

  1. 库中的文件夹中创建My_Db_Profiler_Log类
  2. 添加以下行的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 = "\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); 

} 

} 

?> 
2

enter image description here

这将让你看到SQL查询的网页,它可能是题外话但它有帮助

我强烈建议你使用ZF调试栏,它会给你非常方便的信息 我用它来查看我的教义查询,它支持zend db

https://github.com/jokkedk/ZFDebug