Combines two drivers

Overview

Outputs log events directly on the desktop with the Growl notification system, and to a flat file, using the composite driver of PEAR::Log package

Screenshots

Here we used two different skins (plain on left, smokestack on right) as render samples. It's up to you to choose and use the skin you prefer.

plain skin in action
smokestack skin in action

Dependencies

This example requires mandatory resources :

Explains step by step

Start by creating the individual log handlers on lines 28-29.

We decide to catch PHP errors and exceptions with the Growl handler, on lines 31-32.

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

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

Source Code

PHP code
  1. <?php
  2. /**
  3.  * Composite usage of Growl PEAR::Log driver
  4.  *
  5.  * PHP versions 4 and 5
  6.  *
  7.  * @category  Logging
  8.  * @package   Log
  9.  * @author    Laurent Laville <pear@laurent-laville.org>
  10.  * @copyright 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/growl_composite.php
  15.  */
  16. ?>
  17. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  18. <html>
  19. <head>
  20. <title>Log Growl composite examples</title>
  21. <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
  22. </head>
  23. <body>
  24. <h1>Log Growl</h1>
  25. <?php
  26. require_once 'Log.php';
  27.  
  28. $file  = &Log::singleton('file', 'out.log', 'TEST');
  29. $growl = &Log::singleton('growl', '', 'TEST');
  30. if (isset($growl)) {
  31.     $growl->registerErrorHandler();
  32.     $growl->registerExceptionHandler();
  33. }
  34.  
  35. $composite = &Log::singleton('composite');
  36. $composite->addChild($file);
  37. $composite->addChild($growl);
  38.  
  39. $composite->log('This simple event will be logged to both handlers.');
  40.  
  41. $composite->log(array('message' => "Be carefull message"), PEAR_LOG_WARNING);
  42.  
  43. $var = array('i' => 10, 'j' => 20);
  44. $composite->log(
  45.     array(
  46.         'message'  => var_export($var, true),
  47.         'priority' => GROWL_PRIORITY_MODERATE,
  48.         'sticky'   => true
  49.     )
  50. );
  51.  
  52. // catch a PHP notice error (see registerErrorHandler() function)
  53. print $var['foo'];
  54.  
  55. // conclude with specific PHP5 exception test cases
  56. if (version_compare(phpversion(), '5.0.0', '>=')) {
  57.     function test($arg)
  58.     {
  59.         throw new Exception('Test Exception');
  60.     }
  61.  
  62.     try {
  63.         test(array('Hello'=>'World'));
  64.     } catch(Exception $e) {
  65.         /* Log catchable exception */
  66.         $composite->log($e, PEAR_LOG_ALERT);
  67.     }
  68.  
  69.     throw new Exception('Uncaught Exception');
  70.  
  71.     echo 'not executed';
  72. }
  73. ?>
  74. </body>
  75. </html>
generated by Generic Syntax Highlighter - GeSHi