Return to Snippet

Revision: 35716
at November 12, 2010 20:45 by sadus


Updated Code
// Do you want to debug?
define('debug',true);
error_reporting(E_ALL);
$start_time = microtime(true);
register_shutdown_function('shutdown');

// Include this function to your functions.inc.php or whatever shared libraries you are using.
function debug($trace,$query = null){
    if(debug){
        $caller=array_shift($trace);
        if(isset($caller['class']))
                error_log("Initiating class: " . $caller['class']);
        if(isset($caller['function']))
                error_log("Calling function: " . $caller['function']);
        error_log("In file: " . $caller['file']);
        error_log("@ line: " .$caller['line']);
        if(isset($query)){
            error_log("Performing Query: " .$query);
        }
        error_log("---");
    }
}

function shutdown() {
    global $start_time;
    error_log("Execution took: ".round((microtime(true) - $start_time),4)." seconds.");
}

// Add the following lines in every function:
if(debug){
    debug(debug_backtrace());
}

// Examples:
function clean($string,$type){
    if(debug){
        debug(debug_backtrace());
    }
    doTask();
}

// You can also log all the queries you are using by using debug($query) like this:
function selectQuery($query){
    if(debug){
        debug(debug_backtrace(),$query);
    }
    $q = mysql_query($query,$this->dblink) // Make sure $this->dblink goes to your MySQL Connection
        or die("MySQL Error: " . mysql_error());
    $result = mysql_fetch_assoc($q);
    return $result;
}


// Result looks like this:
[12-Nov-2010 11:43:36] Initiating class: Db
[12-Nov-2010 11:43:36] Calling function: dblink
[12-Nov-2010 11:43:36] In file: /Users/sadus/Dropbox/Code/classes/db.class.php
[12-Nov-2010 11:43:36] @ line: 8
[12-Nov-2010 11:43:36] ---
[12-Nov-2010 11:43:36] Initiating class: Db
[12-Nov-2010 11:43:36] Calling function: selectQuery
[12-Nov-2010 11:43:36] In file: /Users/sadus/Dropbox/Code/classes/usermanagement.class.php
[12-Nov-2010 11:43:36] @ line: 76
[12-Nov-2010 11:43:36] Performing Query: SELECT * 
                    FROM user
                    WHERE email = '' AND verified = '1' LIMIT 1
[12-Nov-2010 11:43:36] ---
[12-Nov-2010 11:43:36] Execution took: 0.0076 seconds.

Revision: 35715
at November 12, 2010 04:47 by sadus


Updated Code
// Do you want to debug?
define('debug',true);
error_reporting(E_ALL);

// Include this function to your functions.inc.php or whatever shared libraries you are using.
function debug($trace,$query = null){
    if(debug){
        $caller=array_shift($trace);
        if(isset($caller['class']))
                error_log("Initiating class: " . $caller['class']);
        if(isset($caller['function']))
                error_log("Calling function: " . $caller['function']);
        error_log("In file: " . $caller['file']);
        error_log("@ line: " .$caller['line']);
        if(isset($query)){
            error_log("Performing Query: " .$query);
        }
        error_log("---");
    }
}

// Add the following lines in every function:
if(debug){
    debug(debug_backtrace());
}

// Examples:
function clean($string,$type){
    if(debug){
        debug(debug_backtrace());
    }
    doTask();
}

// You can also log all the queries you are using by using debug($query) like this:
function selectQuery($query){
    if(debug){
        debug(debug_backtrace(),$query);
    }
    $q = mysql_query($query,$this->dblink) // Make sure $this->dblink goes to your MySQL Connection
        or die("MySQL Error: " . mysql_error());
    $result = mysql_fetch_assoc($q);
    return $result;
}

Revision: 35714
at November 12, 2010 04:46 by sadus


Updated Code
// Do you want to debug?
define('debug',true);
error_reporting(E_ALL);

// Include this function to your functions.inc.php or whatever shared libraries you are using.
function debug($trace,$query = null){
    if(debug){
        $caller=array_shift($trace);
        if(isset($caller['class']))
                error_log("Initiating class: " . $caller['class']);
        if(isset($caller['function']))
                error_log("Calling function: " . $caller['function']);
        error_log("In file: " . $caller['file']);
        error_log("@ line: " .$caller['line']);
        if(isset($query)){
            error_log("Performing Query: " .$query);
        }
        error_log("---");
    }
}

// Add the following lines in every function:
if(debug){
    debug(debug_backtrace());
}

// Examples:
function clean($string,$type){
    if(debug){
        debug(debug_backtrace());
    }

 doBlablabla;
 doMoreblablabla;
}

// You can also log all the queries you are using by using debug($query) like this:
    function selectQuery($query){
        if(debug){
            debug(debug_backtrace(),$query);
        }
        $q = mysql_query($query,$this->dblink) // Make sure $this->dblink goes to your MySQL Connection
            or die("MySQL Error: " . mysql_error());
        $result = mysql_fetch_assoc($q);

        return $result;
    }

Revision: 35713
at November 12, 2010 04:44 by sadus


Updated Code
// Do you want to debug?
define('debug',true);
error_reporting(E_ALL);

// Include this function to your functions.inc.php or whatever shared libraries you are using.
function debug($trace,$query = null){
    if(debug){
        $caller=array_shift($trace);
        if(isset($caller['class']))
                error_log("Initiating class: " . $caller['class']);
        if(isset($caller['function']))
                error_log("Calling function: " . $caller['function']);
        error_log("In file: " . $caller['file']);
        error_log("@ line: " .$caller['line']);
        if(isset($query)){
            error_log("Performing Query: " .$query);
        }
        error_log("---");
    }
}

// Add the following lines in every function:
if(debug){
    debug(debug_backtrace());
}

// Example:
function clean($string,$type){
    if(debug){
        debug(debug_backtrace());
    }

 doBlablabla;
 doMoreblablabla;
}

Revision: 35712
at November 12, 2010 04:44 by sadus


Initial Code
// Do you want to debug?
define('debug',true);

// Include this function to your functions.inc.php or whatever shared libraries you are using.
function debug($trace,$query = null){
    if(debug){
        $caller=array_shift($trace);
        if(isset($caller['class']))
                error_log("Initiating class: " . $caller['class']);
        if(isset($caller['function']))
                error_log("Calling function: " . $caller['function']);
        error_log("In file: " . $caller['file']);
        error_log("@ line: " .$caller['line']);
        if(isset($query)){
            error_log("Performing Query: " .$query);
        }
        error_log("---");
    }
}

// Add the following lines in every function:
if(debug){
    debug(debug_backtrace());
}

// Example:
function clean($string,$type){
    if(debug){
        debug(debug_backtrace());
    }

 doBlablabla;
 doMoreblablabla;
}

Initial URL

                                

Initial Description
I'm originally a sys admin and i love "tail - f /var/log/*" ... so i created a similar function in PHP that will allow me to view in real time what classes/functions/files/lines of my code is being run...

The usage is really simple, just set a global variable "debug" to true/false... 
Note: The usage of this function will slow down your script... use it only to debug. Turn it off on production.

to view the logs, go to your default php error log file. Enjoy!

Initial Title
debug PHP for the newbies in error logs with execution time

Initial Tags
php, debug, error, log

Initial Language
PHP