0

In the past I have used FirePHP to log database calls and responses. I don't want to do this now, since there seems to be a lot of issues surrounding FirePHP and version compatibility.

Are there any alternatives? I don't particularly need to see the logging in my browser console. I'm quite happy to consult log files...

Thanks!

2 Answers 2

2

Zend_Db_Profiler in conjunction with Zend_Log should work for what you want to do. Zend_Db_Profiler also supports console logging to Firebug out of the box.

Here is a plugin I use that summarizes all of the page's queries using the profiler:

<?php

class My_Page_Profiler extends Zend_Controller_Plugin_Abstract
{
    public function dispatchLoopShutdown()
    {
        $db = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('db');
        $profiler = $db->getProfiler();

        $totalQueries = $profiler->getTotalNumQueries();
        $queryTime    = $profiler->getTotalElapsedSecs();

        $longestTime = 0;
        $longestQuery = null;
        $queries = $profiler->getQueryProfiles();

        $content = "Executed $totalQueries database queries in $queryTime seconds<br />\n";

        if ($queries !== false) { // loop over each query issued
            foreach ($queries as $query) {
                $content .= "\n<!-- Query (" . $query->getElapsedSecs() . "s): " . $query->getQuery() . "\n\n -->\n";
                if ($query->getElapsedSecs() > $longestTime) {
                    $longestTime = $query->getElapsedSecs();
                    $longestQuery = htmlspecialchars(addcslashes($query->getQuery(), '"'));
                }
            }

            $content .= "Longest query time: $longestTime."
                       ." <a href=\"#\" onclick=\"return false\" title=\"$longestQuery\">Mouseover for query</a><br />\n";
        }

        // Log $content here, or append it to the response body

        $this->getResponse()->setBody($this->getResponse()->getBody() .
                                      $content);
    }
}

For my purposes, I show the profiler information in the page footer when in development mode, but you can easily change the last line to log $content and restructure content as needed. You can also change it to log each query one at a time rather than building an HTML string out of all the queries. Hopefully this gives you a good working example of how to use Zend_Db_Profiler.

To use it, you need to enable the profiler first, and then register that plugin from bootstrap or application.ini.

There are several ways to enable the profiler, which you can see on the Zend_Db_Profiler introduction.

Hope that helps.

Sign up to request clarification or add additional context in comments.

1 Comment

No worries take your time, hope it proves helpful.
1

The easiest way is to create your own db profiler. e.g. The configuration for using the firebug+firephp profiler in an ini configuration file will look something like this:

resources.db.params.dbname              = "dbname"
resources.db.params.username            = "username"
resources.db.params.password            = "password"
resources.db.params.profiler.enabled    = 1
resources.db.params.profiler.class      = "Zend_Db_Profiler_Firebug"

If we take a look at the Zend_Db_Profiler_Firebug it's really quite a simple piece of code, it extends Zend_Db_Profiler overriding some methods to implement the firebug+firephp logging logic.

So we can create our own custom logging profiler by extending Zend_Db_Profiler. e.g. Our custom profiler might be contained in a custom library: CustomLib_Db_Profiler:

CustomLib_Db_Profile extends Zend_Db_Profiler
{
    // log db profiling to file logic ...
}

Now all we need to do to turn on our custom logging routine is change Zend_Db_Profiler_Firebug to CustomLib_Db_Profiler in our configuration file, like so (of course, this assumes the CustomLib prefix, in this example, in on the include path):

resources.db.params.dbname              = "dbname"
resources.db.params.username            = "username"
resources.db.params.password            = "password"
resources.db.params.profiler.enabled    = 1
resources.db.params.profiler.class      = "CustomLib_Db_Profiler"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.