Combines two drivers

Overview

Log events both to firebug console (through FirePHP) and to a flat file, using the composite driver of PEAR::Log

Screenshot

composite usage 1

Dependencies

This example requires mandatory resources :

Explains step by step

This example log the same type of events as with basic usage, but also log result to a flat file (in this example).

Start by creating the individual log handlers on lines 30-31.

Then, construct a composite handler and add the individual handlers as children of the composite on lines 36-38.

We have just to send events to the composite handler instance to get result on two targets (firebug console and flat file).

Source Code

PHP code
  1. <?php
  2. /**
  3.  * Composite 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_composite.php
  15.  * @link      http://www.laurent-laville.org/img/log/screenshot/Log_FirePHP_example4.png
  16.  *            screenshot (Image PNG, 940x576 pixels) 36.7 Kb
  17.  */
  18. ?>
  19. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  20. <html>
  21. <head>
  22. <title>Log FirePHP composite examples</title>
  23. <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
  24. </head>
  25. <body>
  26. <h1>Log FirePHP</h1>
  27. <?php
  28. require_once 'Log.php';
  29.  
  30. $file    = &Log::singleton('file', 'out.log', 'TEST');
  31. $firephp = &Log::singleton('firephp', '', 'TEST');
  32. if (isset($firephp)) {
  33.     $firephp->registerErrorHandler();
  34. }
  35.  
  36. $composite = &Log::singleton('composite');
  37. $composite->addChild($file);
  38. $composite->addChild($firephp);
  39.  
  40. $composite->log('This event will be logged to both handlers.');
  41.  
  42. $composite->log(array('message' => "Log message", 'label' => "Log message label"));
  43. $composite->log(array('message' => "Info message",
  44.                       'label' => "Info message label",
  45.                       'type'=> PEAR_LOG_FIREPHP_INFO),
  46.                 PEAR_LOG_INFO);
  47. $composite->log(array('message' => "Warning message",
  48.                       'label' => "Warning message label",
  49.                       'type'=> PEAR_LOG_FIREPHP_WARN),
  50.                 PEAR_LOG_WARNING);
  51. $composite->log(array('message' => "Error message",
  52.                       'label' => "Error message label",
  53.                       'type'=> PEAR_LOG_FIREPHP_ERROR),
  54.                 PEAR_LOG_ERR);
  55.  
  56. $composite->log(array('message' => apache_request_headers(),
  57.                       'label' => "RequestHeaders",
  58.                       'type'=> PEAR_LOG_FIREPHP_DUMP),
  59.                 PEAR_LOG_DEBUG);
  60.  
  61. $firephp->log(array('label' => "Backtrace to here",
  62.                     'type'=> PEAR_LOG_FIREPHP_TRACE),
  63.               PEAR_LOG_DEBUG);
  64.  
  65. $table   = array();
  66. $table[] = array('SQL Statement', 'Time', 'Result');
  67. $table[] = array('SELECT * FROM Foo', '0.02', array('row1','row2'));
  68. $table[] = array('SELECT * FROM Bar', '0.04', array('row1','row2'));
  69. $composite->log(array('message' => $table,
  70.                       'label' => '2 SQL queries took 0.06 seconds',
  71.                       'type' => PEAR_LOG_FIREPHP_TABLE),
  72.                 PEAR_LOG_DEBUG);
  73.  
  74. $firephp->log(array('label' => 'Convenience functions call Group',
  75.                     'type' => PEAR_LOG_FIREPHP_GROUP_START));
  76.  
  77. $composite->info(array('message' => "Info convenience message",
  78.                        'label' => "Info convenience message label"));
  79.  
  80. $firephp->log(array('type' => PEAR_LOG_FIREPHP_GROUP_END));
  81.  
  82. $var = array('i' => 10, 'j' => 20);
  83. $composite->log(array('message' => $var, 'label' => 'Iterators'));
  84.  
  85. trigger_error('This is an error log message.', E_USER_ERROR);
  86.  
  87. function test($arg)
  88. {
  89.     throw new Exception('Test Exception');
  90. }
  91.  
  92. try {
  93.     test(array('Hello'=>'World'));
  94. } catch(Exception $e) {
  95.     /* Log exception including stack trace & variables */
  96.     $composite->log($e, PEAR_LOG_ALERT);
  97. }
  98. ?>
  99. </body>
  100. </html>
generated by Generic Syntax Highlighter - GeSHi