简体   繁体   中英

Zend_Db_Profiler log to file

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.

Is there a class I can swap out with Zend_Db_Profiler_Firebug?

UPDATE: See my answer below.

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.

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.

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());
    }
}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM