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

前端之家收集整理的这篇文章主要介绍了zend-framework – Zend启用SQL查询日志记录前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我用它来检索数据库连接atm. @H_403_1@$db = Zend_Db_Table::getDefaultAdapter();

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

@H_403_1@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

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

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

@H_403_1@<?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); } } ?>

猜你在找的PHP相关文章