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.
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
<?php
/**
* Composite usage of Growl PEAR::Log driver
*
* PHP versions 4 and 5
*
* @category Logging
* @package Log
* @author Laurent Laville <pear@laurent-laville.org>
* @copyright 2009 Laurent Laville
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id:$
* @link http://pear.php.net/package/Log
* @example examples/growl_composite.php
*/
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Log Growl composite examples</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Log Growl</h1>
<?php
require_once 'Log.php';
$file = &Log::singleton('file', 'out.log', 'TEST');
$growl = &Log::singleton('growl', '', 'TEST');
if (isset($growl)) {
$growl->registerErrorHandler();
$growl->registerExceptionHandler();
}
$composite = &Log::singleton('composite');
$composite->addChild($file);
$composite->addChild($growl);
$composite->log('This simple event will be logged to both handlers.');
$composite->log(array('message' => "Be carefull message"), PEAR_LOG_WARNING);
$var = array('i' => 10, 'j' => 20);
$composite->log(
array(
'message' => var_export($var, true),
'priority' => GROWL_PRIORITY_MODERATE,
'sticky' => true
)
);
// catch a PHP notice error (see registerErrorHandler() function)
print $var['foo'];
// conclude with specific PHP5 exception test cases
if (version_compare(phpversion(), '5.0.0', '>=')) {
function test($arg)
{
throw new Exception('Test Exception');
}
try {
test(array('Hello'=>'World'));
} catch(Exception $e) {
/* Log catchable exception */
$composite->log($e, PEAR_LOG_ALERT);
}
throw new Exception('Uncaught Exception');
echo 'not executed';
}
?>
</body>
</html>