Aug
21

Zend Framework and Firebug – Log and Debug your Projects

firebugWhen developing an application there are some important factors that you have to pay close attention to avoid problems in the future. I think one of the most important is logging information on how your application is working and when it fails.

Must of us like to log only big exceptions and fatal errors, but the truth is, that when you are in the development process it’s very important to keep track of not just errors, but important information of when something gets executed. As a web developer one of the most important tools to have is FireBug. If you didn’t know, FireBug has an API that you can use to send console messages for logging purposes, when debugging JavaScript. But, did you know you can use FireBug to debug your php applications?

Through Wildfire, a project that was created to help developers have a standard way for sending messages between different programming languages and scripts. The Zend Framework is now able to communicate to the Firebug console in order to display logging messages injected by PHP.

Using this component is pretty straight forward if you are using Zend_Controller_Front (MVC) with zend Framework.

Place this in your bootstrap file before dispatching your front controller

$writer = new Zend_Log_Writer_Firebug();
$logger = new Zend_Log($writer);

Then whenever you want to log something to Firebug just use it in your model, view, or controller like this.

$logger->log('This is an INFORMATIONAL log message!', Zend_Log::INFO);
$logger->log('This is a WARNING log message!', Zend_Log::WARN);
$logger->log('This is an ERROR log message!', Zend_Log::ERROR);

If you notice in the example above, you can specify the type of message you want to log (INFO, WARN, etc..). This is helpful when you want to clearly identify what type of message you are viewing since it will apply a special formatting and in some instances provide more information.

If you want to use the Zend_Log_Writer_Firebug as a stand alone here is the sample code you can use:

//Instantiate the Firebug Writer
$writer = new Zend_Log_Writer_Firebug();
$logger = new Zend_Log($writer);

//start the wildfire component
$request = new Zend_Controller_Request_Http();
$response = new Zend_Controller_Response_Http();
$channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
$channel->setRequest($request);
$channel->setResponse($response);

// Start output buffering
ob_start();

// Now you can make calls to the logger

$logger->log('This is a log message!', Zend_Log::INFO);

// Flush log data to browser
$channel->flush();
$response->sendHeaders();

If you don’t use ZendFramework there is another alternative to php developers. FirePhp, will also enable you to send log messages to Firebug by using php method calls. Be sure to check FirePhp website to get more information.

So remember that this is just one easy and great way to debug your projects, there are many other tools and methods you can incorporate into your projects. If you want to share other ways or simply just want to ask a question regarding this post, feel free to leave a comment.

4 Comments to “Zend Framework and Firebug – Log and Debug your Projects”

  • Great post Julian, keep it up …

  • Thanks Cris..

  • HI, How exatly are you getting $logger object in model ?

    You said "Place this in your bootstrap file before dispatching your front controller"
    and "… to log something to Firebug just use it in your model, view, or controller.."

    You can easilyy access bootstrap object in controller how are you accessing $logger instance in model ?

  • @Andrius

    in your bootstrap class:

    class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
    {
    //set a Log resource
    public function _initLog()
    {
    $logger = new Zend_Log();
    $logger->addWriter(new Zend_Log_Writer_Firebug());
    return $logger;
    }
    }

    Now you can call the resource (Log) from anywhere in you're controllers

    $logger = $this->getInvokeArg('bootstrap')->getResource('Log');

    or from in a model (or elsewhere)

    $logger = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('Log');

Leave a comment