Re: Log observers
| From: | Paul Wolstenholme | Date: | Thu, 10 Jul 2003 16:12:55 +0000 |
| Subject: | Re: Log observers | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-6555@lists.php.net to get a copy of this message | ||
I had a similar problem but I discovered that the pattern was implemented in the Horde project so used that as a guideline. I here are two chunks of code I have be working on:
<?php
include_once 'Log.php';
include_once "Log/observer.php";
include_once "observer_fatconnect.php";
/**
* @package Connect
*/
define('APPLICATION_LOG_DEFAULT_PRIORITY', LOG_INFO);
define('APPLICATION_LOG_MAX_PRIORITY', LOG_DEBUG);
define('APPLICATION_LOG_EMAIL', LOG_ERR);
/* ApplicationLog::logMessage($entry, __CLASS__, __LINE__, $level); */
class ApplicationLog
{
function logMessage($message, $file, $line, $priority = APPLICATION_LOG_DEFAULT_PRIORITY, $email = APPLICATION_LOG_EMAIL)
{
static $logger;
if (!isset($logger)) {
$conf = array('mode' => 0600 ,'timeFormat' => '%x %X');
$logger = Log::singleton ('syslog', 'Syslog', 'FatConnect', $conf, APPLICATION_LOG_MAX_PRIORITY);
//set the lowest level we want to be emailed about
$observer = new Log_observer_fatconnect ($email);
$logger->attach($observer);
if (!$logger || !is_object($logger) || !is_subclass_of($logger, 'log')) {
echo "No logger \n";
if (PEAR::isError($logger)) {
echo "Logger error \n";
} else {
echo "Logger error and ApplicationLog can't log it \n";
}
}
}
$message = $message . ' [on line ' . $line . ' of "' . $file . '"]';
$logger->log($message, $priority);
return true;
}
}
?>
<?php
define('LOG_OBSERVER_EMAIL', 'wolstena@sfu.ca');
/**
* The Log_observer:: class implements the Observer end of a Subject-Observer
* pattern for watching log activity and taking actions on exceptional events.
*
* @author Paul Wolstenholme <wolstena@sfu.ca>
* @package Connection
*/
class Log_observer_fatconnect extends Log_observer
{
/**
*
* @param array $event A hash describing the log event.
*/
function notify($event)
{
$subject = "[FatPort]" . $event['message'];
$body = "An error of priority: " . $event['priority'] . " has occured. \n\n";
$body .= "Where:
LOG_EMERG = 0
LOG_ALERT = 1
LOG_CRIT = 2
LOG_ERR = 3
LOG_WARNING = 4
LOG_NOTICE = 5
LOG_INFO = 6
LOG_DEBUG = 7\n
";
$body .= "The error message returned is: " . $event['message'] . ".\n\n Better get on it :-)";
$mail = mail(LOG_OBSERVER_EMAIL, $subject, $body);
if (!$mail)
{
trigger_error('Log email notification routine has failed', E_USER_WARNING);
}
}
}
?>
On Thursday, July 10, 2003, at 08:38 AM, Mike Carter wrote:
Hi, I'm currently logging words that are entered in to a search box on my web page. At the same time as logging these words to a text file on the server I would like to inform somebody via email. Is this possible via the observer mechanism? The documentation doesn't really expain what it is and assumes you know already it seems! I can't find any sort of in-depth examples of using the PEAR::Log either. Help help help much appreciated on this. Thanks. -- Mike Carter [Senior Web Developer] Personal Medical Advisor Limited. T: 0870 240 7582 // F: 0161 928 2932 E: mike.carter@personalmedicaladvisor.co.uk W: www.personalmedicaladvisor.co.uk ________________________________________________________________________ This e-mail has been scanned for all viruses by Star Internet. The service is powered by MessageLabs. For more information on a proactive anti-virus service working around the clock, around the globe, visit: http://www.star.net.uk ________________________________________________________________________ --PEAR General Mailing List (http://pear.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php