Posted By

uberdragon on 10/15/11


Tagged

firebug-console-with-php


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

bennettstone


Log PHP Errors,Notices,Warnings,Exceptions and Debugging to Firebug Console


 / Published in: PHP
 

URL: http://www.phpclasses.org/package/7181-PHP-AutoLog-PHP-Errors-and-Debug-with-Firebug-Console.html

This PHP Class allows automatic logging all PHP errors to the firebug console. You can also use this class to push information directly to the console from within PHP.

  1. <?php
  2. # This Class will only work on PHP v5.2.0+ so we enforce this here
  3. if (strnatcmp(phpversion(),'5.2.0') < 0) {
  4. $classFileName = end(explode('/',__FILE__));
  5. die($classFileName . ' requires PHP version 5.2++<br><br>Please upgrade PHP to use this Class');
  6. }
  7.  
  8. # Initiate the class upon loading the file
  9. $console = new consoleLog();
  10.  
  11. # Set PHP's error handler to our global function that handles this through the class
  12. set_error_handler("consoleLogPHPError");
  13.  
  14. # Set PHP's exception handler to our global function that handles this through the class
  15. set_exception_handler("consoleLogPHPExceptions");
  16.  
  17. # Set PHP's exit handler to our global function that handles this through the class (allows catching fatal errors)
  18. register_shutdown_function("consoleLogPHPExit");
  19.  
  20. /**
  21.  * class.consoleLog.php
  22.  * Used for logging information to the firebug console via PHP. This file includes three global functions
  23.  * at the end of the file to route set_error_handler(), register_shutdown_function(), and set_exception_handler()
  24.  * which allows the automatic error handling of PHP warnings, errors & exceptions
  25.  *
  26.  * Written by Shane Kretzmann -> http://Shane.Kretzmann.net -> [email protected]
  27.  */
  28. class consoleLog {
  29.  
  30. private $NL; // New Line characters
  31. private $type; // Used internally to allow $var display in the right area
  32. // The following variables hold true or false depending on visibilty wanted.
  33. private $showLog;
  34. private $showInfo;
  35. private $showDebug;
  36. private $showWarn;
  37. private $showError;
  38. private $showException;
  39.  
  40. function __construct() {
  41.  
  42. // Set new line commands
  43. $this->NL = "
  44. ";
  45. // default settings to show all console log types
  46. $this->showLog = $this->showInfo = $this->showDebug = $this->showWarn = $this->showError = $this->showException = true;
  47. }
  48.  
  49. /**
  50. * Class defaults to everything on, use this function to customize what to show in firebug console and what not to
  51. * Pass this function an array of settings to switch things on or off
  52. * ie: array('log'=>true,'info'=>false,'debug'=>true,'warnings'=>true,'errors'=>true,'exceptions'=>true)
  53. */
  54. public function settings($array) {
  55. foreach ($array as $key => $value) {
  56. switch($key) {
  57. case 'log':
  58. $this->showLog = $value;
  59. break;
  60. case 'info':
  61. $this->showInfo = $value;
  62. break;
  63. case 'debug':
  64. $this->showDebug = $value;
  65. break;
  66. case 'warnings':
  67. $this->showWarn = $value;
  68. break;
  69. case 'errors':
  70. $this->showError = $value;
  71. break;
  72. case 'exceptions':
  73. $this->showException = $value;
  74. break;
  75.  
  76. }
  77. }
  78. }
  79.  
  80. /**
  81. * The following public functions simply send different types of console message
  82. * Each function accepts $text which is the message to display and $var which can
  83. * be a string, array or object for display in the console
  84. */
  85. public function log($text,$var=null) {
  86. if ($this->showLog) return $this->__processConsoleLog($text,$var,1);
  87. }
  88. public function info($text,$var=null) {
  89. if ($this->showInfo) return $this->__processConsoleLog($text,$var,2);
  90. }
  91. public function warn($text,$var=null) {
  92. if ($this->showWarn) return $this->__processConsoleLog($text,$var,3);
  93. }
  94. public function exception($text,$var=null) {
  95. if ($this->showException) return $this->__processConsoleLog($text,$var,4);
  96. }
  97. public function error($text,$var=null) {
  98. if ($this->showError) return $this->__processConsoleLog($text,$var,4);
  99. }
  100. public function debug($text,$var=null) {
  101. if ($this->showDebug) return $this->__processConsoleLog($text,$var,5);
  102. }
  103.  
  104. /**
  105. * This function is the core of the class and creates the necessary
  106. * javascript to push the information being passed to the firebug console
  107. * It should only be called by an internal class function
  108. */
  109. private function __processConsoleLog($name, $var = null, $type = 1) {
  110. echo $this->NL . '<script type="text/javascript">' . $this->NL;
  111. // We need to remove any carriage returns or new lines from within the $name variable
  112. $name = str_replace(array(chr(13),chr(10)),'',$name);
  113.  
  114. switch($type) {
  115. case 1: // Push Log to firebug console
  116. echo 'console.log("'.$name.'");'.$this->NL;
  117. $this->type = 'log';
  118. break;
  119. case 2: // Push Info to firebug console
  120. echo 'console.info("'.$name.'");'.$this->NL;
  121. $this->type = 'info';
  122. break;
  123. case 3: // Push Warning to firebug console
  124. echo 'console.warn("'.$name.'");'.$this->NL;
  125. $this->type = 'warn';
  126. break;
  127. case 4: // Push Error to firebug console
  128. echo 'console.error("'.$name.'");'.$this->NL;
  129. $this->type = 'error';
  130. break;
  131. case 5: // Push Debug to firebug console
  132. echo 'console.debug("'.$name.'");'.$this->NL;
  133. $this->type = 'debug';
  134. break;
  135.  
  136. }
  137. if (!empty($var)) {
  138. if (is_object($var) || is_array($var)) {
  139. $object = json_encode($var);
  140. echo 'var object'.preg_replace('~[^A-Z|0-9]~i',"_",$name).' = \''.str_replace("'","\'",$object).'\';'.$this->NL;
  141. echo 'var val'.preg_replace('~[^A-Z|0-9]~i',"_",$name).' = eval("(" + object'.preg_replace('~[^A-Z|0-9]~i',"_",$name).' + ")" );'.$this->NL;
  142. echo 'console.'.$this->type.'(val'.preg_replace('~[^A-Z|0-9]~i',"_",$name).');'.$this->NL;
  143. } else { // not an object or array so we will just pass it to the console as a string
  144. echo 'console.'.$this->type.'("'.str_replace('"','\\"',$var).'");'.$this->NL;
  145. }
  146. }
  147. echo '</script>'.$this->NL;
  148. }
  149. }
  150.  
  151. /**
  152. * Push PHP Errors to FireBug Console
  153. */
  154. function consoleLogPHPError($errno,$errstr,$errfile,$errline) {
  155. global $console; // pull in $console object
  156. switch ($errno) {
  157. case E_NOTICE:
  158. case E_USER_NOTICE:
  159. $errorType = "Notice";
  160. $consoleType = "warn";
  161. break;
  162. case E_WARNING:
  163. case E_USER_WARNING:
  164. $errorType = "Warning";
  165. $consoleType = "warn";
  166. break;
  167. case E_ERROR:
  168. case E_USER_ERROR:
  169. $errorType = "Fatal Error";
  170. $consoleType = "error";
  171. break;
  172. default:
  173. $errorType = "Unknown";
  174. $consoleType = "warn";
  175. break;
  176. }
  177. $errstr = str_replace(' ',' ',preg_replace('/\[.*?\]:/i','',$errstr)); // no need for the function link back in console log
  178. if (is_object($console)) $console->$consoleType('[PHP '.$errorType.'][' . $errfile . ':' . $errline . '] '.str_replace("'","\'",$errstr));
  179. if (ini_get('log_errors')) error_log(sprintf("PHP %s: %s in %s on line %d", $errorType, $errstr, $errfile, $errline));
  180. }
  181.  
  182. /**
  183. * Global Function to Push PHP Fatal Errors to FireBug Console
  184. */
  185. function consoleLogPHPExit() {
  186. global $console; // pull in $console object
  187. if ($err = error_get_last()) {
  188. $errstr = $err['message'];
  189. $errfile = $err['file'];
  190. $errline = $err['line'];
  191. $errstr = str_replace(' ',' ',preg_replace('/\[.*?\]:/i','',$errstr)); // no need for the href link here
  192. if (is_object($console)) $console->error('[PHP Fatal Error][' . $errfile . ':' . $errline . '] '.str_replace("'","\'",$errstr));
  193. }
  194. }
  195.  
  196. /**
  197. * Global Function to Push PHP UnCaught Exceptions to FireBug Console
  198. */
  199. function consoleLogPHPExceptions($e) {
  200. global $console; // pull in $console object
  201. $trace = $e->getTrace();
  202. $errstr = $e->getMessage();
  203. if (is_object($console)) $console->exception('[PHP Exception]: '.str_replace("'","\'",$errstr),$trace);
  204. }
  205. ?>

Report this snippet  

You need to login to post a comment.