PHP Debugging in Chrome

PHP Debugging in Chrome

PHP Debugging Console with Chrome As I’m pulling together my PHP code, I’m finding that I want a better way of debugging interactively. Right now, I just need one-way interactive error reporting, and using PHP Error logs doesn’t really cut it.

After digging around, I came across ChromePhp, which works with my preferred browser. It’s easy to install:

  1. Download the ChromePhp.php file to your server
  2. include 'ChromePhp.php' at the top of your code
  3. Use ChromePhp::log("blah blah blah") to write to the Chrome Javascript console.

For output that is longer than 4K, you need to set up a /tmp/chromelogs directory with write permissions for PHP (usually this is the apache user) and then symlink this from within your webroot. Make sure that Options +FollowSymlinks is enabled (check your .htaccess file).

So far, it seems to work. The first thing I did was wrap it in my own debugger class, which looks like this:

<?php // SeahDebugger.php

if (defined('CHROME_DEBUGGING')) {
    include 'ChromePhp.php';
    ChromePhp::useFile('/tmp/chromelogs','/second_logs');
    SDBG::$enabled = true;
}

// Seah ChromePHP Wrapper
class SDBG {
    
    // class variables
    public static $enabled = false;
    
    // simple wrappers
    public static function out () { 
        if (!self::$enabled) return;
        $args = func_get_args();
        if (func_num_args()==1) ChromePhp::log($args[0]); 
        else ChromePhp::log($args);
    }
    public static function warn () { 
        if (!self::$enabled) return;
        $args = func_get_args();
        if (func_num_args()==1) ChromePhp::warn($args[0]); 
        else ChromePhp::warn($args);
    }
    public static function error () { 
        if (!self::$enabled) return;
        $args = func_get_args();
        if (func_num_args()==1) ChromePhp::error($args[0]); 
        else ChromePhp::error($args);
    }
    
    // backtraces
    public static function trace ( $out ) { 
        if (!self::$enabled) return; 
        $backtrace = debug_backtrace();
        extract($backtrace[0]);
        ChromePhp::log( "$out [" . basename($file).":$line:]");
    }
}

This is called by my current version of functions.php, which I consider the start of execution for my burgeoning application:

<?php // functions.php

/** INITIALIZE DEBUGGING *********************************************/
define ('CHROME_DEBUGGING',true);
include_once 'code/libs/SeahDebug.php';


/** GLOBAL STATE DETECTION and CONSTANTS *****************************/

define ( PARENT_PATH, get_template_directory() );
define ( CHILD_PATH, get_stylesheet_directory() );
define ( CHILD_URI, get_stylesheet_directory_uri() );

// global dseah app class instance
global $dsApp;  


/** LOADER ************************************************************/

if ( is_admin() ) {
    require_once ( 'code/admin/admin.php' );
} else {
    require_once ( 'code/_main.php' );
    add_action('init','dseah_main');
    // execution resumes in _main
}

/** END OF EXECUTION **************************************************/

The main reason I wrapped the class was because I have my own set of console debugger commands, and I wanted to add the backtracing feature in a way that I liked. I also can enable simple on/off debug message support which can be expanded to authenticate based on user. There’s probably some security thing I’m missing here, but the important thing is that I have the ability see output in the Javascript console in my browser.

I’m hopeful that this will help me debug webservices on the server side of things, which I’ve never done before. And being able to log Javascript errors in the same console window will be helpful (I think) for AJAX stuff. Another area of mystery!

0 Comments