PHP Classes
Icontem

File: _classes/Logger.class.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Marius Zadara  >  Logger  >  _classes/Logger.class.php  
File: _classes/Logger.class.php
Role: Class source
Content type: text/plain
Description: Main class for logger
Class: Logger
Filter and export data of application events
 

Contents

Class file image Download
<?php

/**
 * Main class definition.
 * This class will implement the logger, adding the handlers and exporters.
 * The handlers will be used to filter the event, and the exporters to save the event.
 * Each handler has one exporter.
 * 
 * @author Marius Zadara <marius@zadara.org>
 * @category org.zadara.marius.logger.classes
 * @copyright (C) 2008 Marius Zadara <marius@zadara.org>
 * @license GNU GPL
 * @package org.zadara.marius.logger
 */
class Logger implements ILogger 
{
	/**
	 * Logger's event handlers.
	 * Based on these handlers, the export of the event will be decided.
	 *
	 * @access protected
	 * @see EventHandler
	 */
	protected $eventHandlers;
	
	
	/**
	 * Default constructor.
	 * It sets the event handlers.
	 * 
	 * @return Logger
	 */
	public function __construct()
	{
		// init the event handlers
		$this->eventHandlers = null;
	}
		
	
	/**
	 * Register handler method.
	 * The function method will add a new event handler to the array,
	 * only if is valid
	 *
	 * @access public
	 * @param EventHandler $eventHandler The new event handler
	 * @return void
	 */
	public function registerHandler($eventHandler)
	{
		// init the handlers array 
		// if not already done
		if (is_null($this->eventHandlers))
			$this->eventHandlers = array();

		// check the handler and add it to the array
		if ($eventHandler instanceof EventHandler)	
			$this->eventHandlers[] = $eventHandler;
	}
	

	/**
	 * Add event method.
	 * The function method will add a new event to the logger,
	 * and will check all the handlers for acceptance.
	 * Throws LoggerException in case of event handlers not already set.
	 * 
	 *
	 * @access public
	 * @param Event $event The new event to handle
	 * @return void
	 */
	public function addEvent($newEvent)
	{
		// check the event handlers
		if (is_null($this->eventHandlers))		
			throw new LoggerException("No event handlers defined");

		// get the size of the event handlers
		$eventHandlersCount = sizeof($this->eventHandlers); 	

		// check the size
		if ($eventHandlersCount == 0)
			throw new LoggerException("No event handlers defined");
		
		// get each handler from the list
		for ($i=0; $i < $eventHandlersCount; $i++)
		{
			// check for event acceptance on the current handler
			// if the event is not accepted, go to the next handler
			if (!$this->eventHandlers[$i]->isEventAccepted($newEvent))
				continue;

			// this point is reached only if the current handler accepts the event
			// in this case, (try to) get the handler's exporter and call the method on it that will export the event
			// TODO: do define a safe exporter, like a console, to export the event in case of error
			try
			{
				$exporter = $this->eventHandlers[$i]->getExporter();
				$exporter->doExport($newEvent);
			}
			catch (ExporterException $exporterException)
			{
				// go on to the next handler
				// althought it will be better to have a "safe" handler to export this event 
				// and not to loose it
				continue;				
			}
			catch (Exception $generalException)
			{
				// go on to the next handler
				// althought it will be better to have a "safe" handler to export this event 
				// and not to loose it				
				continue;				
			}
		}
	}
	
	
	/**
	 * Method to add an event with FINEST level.
	 *
	 * @param String $id The event id. Default null.
	 * @param Source $source The event source. Default null.
	 * @param Category $category The event category. Default null.
	 * @param String $message The event message. Default null.
	 * @param Array $extraFields The event extra fields. Default null.
	 * 
	 * @see EventIDs
	 * @see Sources
	 * @see Categories
	 * @see addEvent()
	 */	
	public function finest($id = null, $source = null, $category = null, $message = null, $extraFields = null)
	{
		// create the new event
		$event = new Event($id, $source, $category, Levels::$FINEST, $message, $extraFields);
		
		// add the event
		$this->addEvent($event);
	}
	
	/**
	 * Method to add an event with FINER level.
	 *
	 * @param String $id The event id. Default null.
	 * @param Source $source The event source. Default null.
	 * @param Category $category The event category. Default null.
	 * @param String $message The event message. Default null.
	 * @param Array $extraFields The event extra fields. Default null.
	 *  
	 * @see EventIDs
	 * @see Sources
	 * @see Categories
	 * @see addEvent()
	 */	
	public function finer($id = null, $source = null, $category = null, $message = null, $extraFields = null)
	{
		// create the new event
		$event = new Event($id, $source, $category, Levels::$FINER, $message, $extraFields);
		
		// add the event
		$this->addEvent($event);
	}
	

	/**
	 * Method to add an event with FINE level.
	 *
	 * @param String $id The event id. Default null.
	 * @param Source $source The event source. Default null.
	 * @param Category $category The event category. Default null.
	 * @param String $message The event message. Default null.
	 * @param Array $extraFields The event extra fields. Default null.
	 *  
	 * @see EventIDs
	 * @see Sources
	 * @see Categories
	 * @see addEvent()
	 */	
	public function fine($id = null, $source = null, $category = null, $message = null, $extraFields = null)
	{
		// create the new event
		$event = new Event($id, $source, $category, Levels::$FINE, $message, $extraFields);
		
		// add the event
		$this->addEvent($event);
	}
	

	/**
	 * Method to add an event with INFO level.
	 *
	 * @param String $id The event id. Default null.
	 * @param Source $source The event source. Default null.
	 * @param Category $category The event category. Default null.
	 * @param String $message The event message. Default null.
	 * @param Array $extraFields The event extra fields. Default null.
	 *  
	 * @see EventIDs
	 * @see Sources
	 * @see Categories
	 * @see addEvent()
	 */	
	public function info($id = null, $source = null, $category = null, $message = null, $extraFields = null)
	{
		// create the new event
		$event = new Event($id, $source, $category, Levels::$INFO, $message, $extraFields);
		
		// add the event
		$this->addEvent($event);
	}

	
	/**
	 * Method to add an event with WARNING level.
	 *
	 * @param String $id The event id. Default null.
	 * @param Source $source The event source. Default null.
	 * @param Category $category The event category. Default null.
	 * @param String $message The event message. Default null.
	 * @param Array $extraFields The event extra fields. Default null.
	 *  
	 * @see EventIDs
	 * @see Sources
	 * @see Categories
	 * @see addEvent()
	 */	
	public function warning($id = null, $source = null, $category = null, $message = null, $extraFields = null)
	{
		// create the new event
		$event = new Event($id, $source, $category, Levels::$WARNING, $message, $extraFields);
		
		// add the event
		$this->addEvent($event);
	}
	

	
	/**
	 * Method to add an event with ERROR level.
	 *
	 * @param String $id The event id. Default null.
	 * @param Source $source The event source. Default null.
	 * @param Category $category The event category. Default null.
	 * @param String $message The event message. Default null.
	 * @param Array $extraFields The event extra fields. Default null.
	 *  
	 * @see EventIDs
	 * @see Sources
	 * @see Categories
	 * @see addEvent()
	 */	
	public function error($id = null, $source = null, $category = null, $message = null, $extraFields = null)
	{
		// create the new event
		$event = new Event($id, $source, $category, Levels::$ERROR, $message, $extraFields);
		
		// add the event
		$this->addEvent($event);
	}

	
	/**
	 * Method to add an event with SEVERE level.
	 *
	 * @param String $id The event id. Default null.
	 * @param Source $source The event source. Default null.
	 * @param Category $category The event category. Default null.
	 * @param String $message The event message. Default null.
	 * @param Array $extraFields The event extra fields. Default null.
	 *  
	 * @see EventIDs
	 * @see Sources
	 * @see Categories
	 * @see addEvent()
	 */	
	public function severe($id = null, $source = null, $category = null, $message = null, $extraFields = null)
	{
		// create the new event
		$event = new Event($id, $source, $category, Levels::$SEVERE, $message, $extraFields);
		
		// add the event
		$this->addEvent($event);
	}
	
	
	
	/**
	 * Method to add an event with CRITICAL level.
	 *
	 * @param String $id The event id. Default null.
	 * @param Source $source The event source. Default null.
	 * @param Category $category The event category. Default null.
	 * @param String $message The event message. Default null.
	 * @param Array $extraFields The event extra fields. Default null.
	 *  
	 * @see EventIDs
	 * @see Sources
	 * @see Categories
	 * @see addEvent()
	 */	
	public function critical($id = null, $source = null, $category = null, $message = null, $extraFields = null)
	{
		// create the new event
		$event = new Event($id, $source, $category, Levels::$CRITICAL, $message, $extraFields);
		
		// add the event
		$this->addEvent($event);
	}
	
	
	
	/**
	 * Method to add an event with CRASH level.
	 *
	 * @param $id The event id. Default null.
	 * @param $source The event source. Default null.
	 * @param $category The event category. Default null.
	 * @param $message The event message. Default null.
	 * @param $extraFields The event extra fields. Default null.
	 * 
	 * @see EventIDs
	 * @see Sources
	 * @see Categories
	 * @see addEvent()
	 */	
	
	
	/**
	 * Method to add an event with CRASH level.
	 *
	 * @param String $id The event id. Default null.
	 * @param Source $source The event source. Default null.
	 * @param Category $category The event category. Default null.
	 * @param String $message The event message. Default null.
	 * @param Array $extraFields The event extra fields. Default null.
	 *  
	 * @see EventIDs
	 * @see Sources
	 * @see Categories
	 * @see addEvent()
	 */	
	
	public function crash($id = null, $source = null, $category = null, $message = null, $extraFields = null)
	{
		// create the new event
		$event = new Event($id, $source, $category, Levels::$CRASH, $message, $extraFields);
		
		// add the event
		$this->addEvent($event);
	}
}

?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products