debug PHP for the newbies in error logs with execution time


/ Published in: PHP
Save to your folder(s)

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!


Copy this code and paste it in your HTML
  1. // Do you want to debug?
  2. define('debug',true);
  3. $start_time = microtime(true);
  4.  
  5. // Include this function to your functions.inc.php or whatever shared libraries you are using.
  6. function debug($trace,$query = null){
  7. if(debug){
  8. $caller=array_shift($trace);
  9. if(isset($caller['class']))
  10. error_log("Initiating class: " . $caller['class']);
  11. if(isset($caller['function']))
  12. error_log("Calling function: " . $caller['function']);
  13. error_log("In file: " . $caller['file']);
  14. error_log("@ line: " .$caller['line']);
  15. if(isset($query)){
  16. error_log("Performing Query: " .$query);
  17. }
  18. error_log("---");
  19. }
  20. }
  21.  
  22. function shutdown() {
  23. global $start_time;
  24. error_log("Execution took: ".round((microtime(true) - $start_time),4)." seconds.");
  25. }
  26.  
  27. // Add the following lines in every function:
  28. if(debug){
  29. debug(debug_backtrace());
  30. }
  31.  
  32. // Examples:
  33. function clean($string,$type){
  34. if(debug){
  35. debug(debug_backtrace());
  36. }
  37. doTask();
  38. }
  39.  
  40. // You can also log all the queries you are using by using debug($query) like this:
  41. function selectQuery($query){
  42. if(debug){
  43. debug(debug_backtrace(),$query);
  44. }
  45. $q = mysql_query($query,$this->dblink) // Make sure $this->dblink goes to your MySQL Connection
  46. or die("MySQL Error: " . mysql_error());
  47. $result = mysql_fetch_assoc($q);
  48. return $result;
  49. }
  50.  
  51.  
  52. // Result looks like this:
  53. [12-Nov-2010 11:43:36] Initiating class: Db
  54. [12-Nov-2010 11:43:36] Calling function: dblink
  55. [12-Nov-2010 11:43:36] In file: /Users/sadus/Dropbox/Code/classes/db.class.php
  56. [12-Nov-2010 11:43:36] @ line: 8
  57. [12-Nov-2010 11:43:36] ---
  58. [12-Nov-2010 11:43:36] Initiating class: Db
  59. [12-Nov-2010 11:43:36] Calling function: selectQuery
  60. [12-Nov-2010 11:43:36] In file: /Users/sadus/Dropbox/Code/classes/usermanagement.class.php
  61. [12-Nov-2010 11:43:36] @ line: 76
  62. [12-Nov-2010 11:43:36] Performing Query: SELECT *
  63. FROM user
  64. WHERE email = '' AND verified = '1' LIMIT 1
  65. [12-Nov-2010 11:43:36] ---
  66. [12-Nov-2010 11:43:36] Execution took: 0.0076 seconds.

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.