Log events to firebug console

Overview

This example show logging messages with priority styling, tables and traces. It also display how to catch errors and exceptions.

Screenshot

basic usage 1

dump variable result

Dependencies

This example requires mandatory resources :

Explains step by step

With the required library included at line 32, we instanciated a new instance of PEAR::Log firephp object line 34. We are now ready to log any event to the firebug console.

At line 38 we decide to catch all PHP errors with the default error handler provided by the PEAR::Log FirePHP driver. This error handler obey at error_reporting() order.

At line 39 we decide to catch all exceptions thrown with the default exception handler provided by the PEAR::Log FirePHP driver. This exception handler just log event at a PEAR_LOG_ALERT level.

At line 40 we decide also to log all failed assertions raised by standard PHP assert functions with the default assertion handler provided by the PEAR::Log FirePHP driver. This assertion handler just log result at a PEAR_LOG_ERR level.

At line 44 we show how to log an implicit message without label

Lines 50-53 we show how to log an explicit message with label and type. DO NOT match FirePHP type message and PEAR::Log priority level. Its not the same thing!

Lines 68-71 we show how to dump content of a variable on the net firebug tab See second screenshot as result.

Lines 74-76 we show how to display back trace as an collapsed table

Lines 79-86 we show how to display (for example) an SQL query with its result set into a collapsed table.

Lines 89-110 we show how to display group messages using convenience functions (log, info, warning, err, dump, trace, table) without to specify the FirePHP type of message (PEAR_LOG_FIREPHP_* constants). Lines 89-90 and 110 are only used by FirePHP and are ignored by all other PEAR::Log drivers (no notified)

Source Code

PHP code
  1. <?php
  2. /**
  3.  * Basic usage of FirePHP PEAR::Log driver
  4.  *
  5.  * PHP version 5
  6.  *
  7.  * @category  Logging
  8.  * @package   Log
  9.  * @author    Laurent Laville <pear@laurent-laville.org>
  10.  * @copyright 2008-2009 Laurent Laville
  11.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  12.  * @version   CVS: $Id:$
  13.  * @link      http://pear.php.net/package/Log
  14.  * @example   examples/firephp_basic.php
  15.  * @link      http://www.laurent-laville.org/img/log/screenshot/Log_FirePHP_example1.png
  16.  *            screenshot (Image PNG, 940x578 pixels) 39.6 Kb
  17.  * @link      http://www.laurent-laville.org/img/log/screenshot/Log_FirePHP_example2.png
  18.  *            screenshot (Image PNG, 941x527 pixels) 36.4 Kb
  19.  * @link      http://www.laurent-laville.org/img/log/screenshot/Log_FirePHP_example3.png
  20.  *            screenshot (Image PNG, 942x283 pixels) 13.1 Kb
  21.  */
  22. ?>
  23. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  24. <html>
  25. <head>
  26. <title>Log FirePHP basic examples</title>
  27. <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
  28. </head>
  29. <body>
  30. <h1>Log FirePHP</h1>
  31. <?php
  32. require 'Log.php';
  33.  
  34. $logger = &Log::singleton('firephp', '', 'ident');
  35. if (!isset($logger)) {
  36.     die('PEAR::Log firephp driver cannot be loaded. Check your install');
  37. }
  38. $logger->registerErrorHandler();
  39. $logger->registerExceptionHandler();
  40. $logger->registerAssertionHandler();
  41. //$logger->setMask(PEAR_LOG_NONE);
  42.  
  43. // 1. A FirePHP LOG implicit message without label
  44. $logger->log(array('message' => "Hello World"));
  45.  
  46. // 2. A FirePHP LOG implicit message with label
  47. $logger->log(array('message' => "Log message", 'label' => "Log message label"));
  48.  
  49. // 3. A FirePHP INFO explicit message with label
  50. $logger->log(array('message' => "Info message",
  51.                    'label' => "Info message label",
  52.                    'type'=> PEAR_LOG_FIREPHP_INFO),
  53.              PEAR_LOG_INFO);
  54.  
  55. // 4. A FirePHP WARNING explicit message with label
  56. $logger->log(array('message' => "Warning message",
  57.                    'label' => "Warning message label",
  58.                    'type'=> PEAR_LOG_FIREPHP_WARN),
  59.              PEAR_LOG_WARNING);
  60.  
  61. // 5. A FirePHP ERROR explicit message with label
  62. $logger->log(array('message' => "Error message",
  63.                    'label' => "Error message label",
  64.                    'type'=> PEAR_LOG_FIREPHP_ERROR),
  65.              PEAR_LOG_ERR);
  66.  
  67. // 6. A FirePHP DUMP explicit message with label
  68. $logger->log(array('message' => apache_request_headers(),
  69.                    'label' => "RequestHeaders",
  70.                    'type'=> PEAR_LOG_FIREPHP_DUMP),
  71.              PEAR_LOG_DEBUG);
  72.  
  73. // 7. A FirePHP TRACE explicit message with label
  74. $logger->log(array('label' => "Backtrace to here",
  75.                    'type'=> PEAR_LOG_FIREPHP_TRACE),
  76.              PEAR_LOG_DEBUG);
  77.  
  78. // 8. A FirePHP TABLE explicit message with label
  79. $table   = array();
  80. $table[] = array('SQL Statement', 'Time', 'Result');
  81. $table[] = array('SELECT * FROM Foo', '0.02', array('row1','row2'));
  82. $table[] = array('SELECT * FROM Bar', '0.04', array('row1','row2'));
  83. $logger->log(array('message' => $table,
  84.                    'label' => '2 SQL queries took 0.06 seconds',
  85.                    'type' => PEAR_LOG_FIREPHP_TABLE),
  86.              PEAR_LOG_DEBUG);
  87.  
  88. // 9. A FirePHP GROUP messages using convenience functions
  89. $logger->log(array('label' => 'Convenience functions call Group',
  90.                    'type' => PEAR_LOG_FIREPHP_GROUP_START));
  91. // see also 3.
  92. $logger->info(array('message' => "Info convenience message",
  93.                    'label' => "Info convenience message label"));
  94. // see also 4.
  95. $logger->warning(array('message' => "Warning convenience message",
  96.                        'label' => "Warning convenience message label"));
  97. // see also 5.
  98. $logger->err(array('message' => "Error convenience message",
  99.                    'label' => "Error convenience message label"));
  100. // see also 6.
  101. $logger->dump(array('message' => apache_request_headers(),
  102.                     'label' => "RequestHeaders"));
  103. // see also 7.
  104. $logger->trace(array('label' => "Convenience Backtrace to here"));
  105. // see also 8.
  106. $logger->table(array('message' => $table,
  107.                      'label' => '2 SQL queries took 0.06 seconds'),
  108.                PEAR_LOG_INFO);
  109. // end of FirePHP GROUP messages
  110. $logger->log(array('type' => PEAR_LOG_FIREPHP_GROUP_END));
  111.  
  112. // 10. A FirePHP LOG explicit message to display variable content
  113. $var = array('i' => 10, 'j' => 20);
  114. $logger->log(array('message' => $var, 'label' => 'Iterators'));
  115.  
  116. // 11. catch a PHP notice error (see registerErrorHandler() function)
  117. print $var['foo'] . PHP_EOL;  // raise a PHP notice , catched by driver error handler
  118.  
  119. // 12. catch a user error (see registerErrorHandler() function)
  120. trigger_error('This is an error log message.', E_USER_ERROR);
  121.  
  122. // 13. catch an assertion (see registerAssertionHandler() function)
  123. assert('mysql_query("")'); // Make an assertion that should fail
  124.  
  125. // 14. conclude with specific PHP5 exception test cases
  126. function test($arg)
  127. {
  128.     throw new Exception('Test Exception');
  129. }
  130.  
  131. try {
  132.     test(array('Hello'=>'World'));
  133. } catch(Exception $e) {
  134.     /* Log exception including stack trace & variables */
  135.     $logger->log($e, PEAR_LOG_ALERT);
  136. }
  137.  
  138. throw new Exception('Uncaught Exception');
  139.  
  140. echo 'not executed';
  141. ?>
  142. </body>
  143. </html>
generated by Generic Syntax Highlighter - GeSHi