Zend_Db_Profiler日志到文件 [英] Zend_Db_Profiler log to file
问题描述
是否有一种简单的方法来记录对文件的查询?我的Firebug分析工作正常:
Is there a simple way to log to queries to a file? I have Firebug profiling working no problem with:
resources.db.params.profiler.enabled = "true"
resources.db.params.profiler.class = "Zend_Db_Profiler_Firebug"
最好将其记录到文件中,而无需编写大量代码.
It would be nice to log this to a file with out writing a bunch of code.
是否有可以与Zend_Db_Profiler_Firebug交换的课程?
Is there a class I can swap out with Zend_Db_Profiler_Firebug?
更新:请参见下面的答案.
UPDATE: See my answer below.
推荐答案
从ZF 1.11.11开始,没有内置的探查器类会将查询记录到文件中.当前,FireBug是唯一的专用Db Profiler.
As of ZF 1.11.11 there is no built in profiler class that will log queries to a file. Currently, FireBug is the only specialized Db Profiler.
这里有两种方法可以解决此问题,而无需加载额外的代码.
Here are two ways in which you can solve it without loads of extra code though.
首先,请查看此答案,因为它显示了如何扩展Zend_Db_Profiler
使其将查询记录到queryEnd
上的文件.如果它不能完全满足您的要求,则可以扩展Zend_Db_Profiler
并使用提供的代码作为起点.
First, check out this answer as it shows how to extend Zend_Db_Profiler
to have it log the queries to a file on queryEnd
. If it doesn't quite do what you want, you can extend Zend_Db_Profiler
and use the provided code as a starting point.
下一个示例是对我在某些应用程序中使用的插件的略微修改,当我在开发该应用程序时,该插件用于剖析查询.此方法利用dispatchLoopShutdown()
插件获取Db Profiler的实例并将查询记录到文件中.
This next example is a slight modification of a plugin I have in some of my applications that I use to profile queries when the application is in development. This method utilizes a dispatchLoopShutdown()
plugin to get an instance of the Db Profiler and log the queries to a file.
<?php /* library/My/Page/DbLogger.php */
class My_Page_DbLogger extends Zend_Controller_Plugin_Abstract
{
public function dispatchLoopShutdown()
{
$db = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('db');
$profiler = $db->getProfiler();
if ($profiler === NULL || !($profiler instanceof Zend_Db_Profiler))
return;
// either create your logger here based on config in application.ini
// or create it elsewhere and store it in the registry
$logger = Zend_Registry::get('dblog');
$totalQueries = $profiler->getTotalNumQueries();
$queryTime = $profiler->getTotalElapsedSecs();
$longestTime = 0;
$queries = $profiler->getQueryProfiles();
if ($queries !== false) {
$content = "\nExecuted $totalQueries database queries in $queryTime seconds<br />\n";
foreach ($queries as $query) {
// TODO: You could use custom logic here to log only selected queries
$content .= "Query (" . $query->getElapsedSecs() . "s): " . $query->getQuery() . "\n";
if ($query->getElapsedSecs() > $longestTime) {
$longestTime = $query->getElapsedSecs();
}
}
$content .= "Longest query time: $longestTime.\n" . str_repeat('-', 80);
$logger->info($content);
}
}
}
要激活此插件,您可以在引导程序中使用以下代码:
To activate this plugin, you can use code like this in your bootstrap:
/**
* Register the profiler if we are running in a non-production mode
*/
protected function _initPageProfiler()
{
if (APPLICATION_ENV == 'development') {
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new My_Page_DbLogger());
}
}
从长远来看,理想情况下,您可能希望创建一个扩展Zend_Db_Profiler
的类,并允许在配置中指定其他选项,例如日志文件路径,日志优先级.这样,您可以将现有过滤器用于Zend_Db_Profiler
.
Ideally, in the long term, you would probably want to make a class that extends Zend_Db_Profiler
and allow additional options to be specified in your config such as the log file path, the log priority. This way you can leverage the existing filters to Zend_Db_Profiler
.
这篇关于Zend_Db_Profiler日志到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!