<?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);
}
}
?>
|