Old*_*dek 7 zend-framework zend-db-table zend-db
我用它来检索数据库连接atm.
$db = Zend_Db_Table::getDefaultAdapter();
Run Code Online (Sandbox Code Playgroud)
我在我的配置中设置了这样:
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
Run Code Online (Sandbox Code Playgroud)
我想将所有内容输出到sql.log中.这可以应用于默认适配器吗?例如通过设置,所以我可以在生产环境中忽略它?
非常感兴趣.
我确实看过:如何使用Zend_Db启用SQL输出到日志文件?但它似乎没有涵盖我的问题.
/马库斯
ero*_*eev 13
有一个扩展Zend_Db_Profiler的示例,因此您可以将查询写入/logs/db-queries.log文件.
所以你必须做以下事情:
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);
}
}
?>
Run Code Online (Sandbox Code Playgroud)
扩展 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);
}
}
Run Code Online (Sandbox Code Playgroud)
还没有测试过,但应该可以解决问题。尝试一下并告诉我。
顺便说一句,你知道你也可以记录 mysql 上的所有查询吗?
| 归档时间: |
|
| 查看次数: |
7779 次 |
| 最近记录: |