Location: PHPKode > projects > Emailr > common/Logger.inc
<?

/**
 * This file contains classes used to aid with logging inside the Emailr codebase.
 *
 * This file provides classes used to aid in logging inside the Emailr codebase.  
 * Logging can aid in debugging, finding trends, sensing areas of the application that 
 * need improvements in performance, debugging, and lastly debugging.
 * @package Emailr
 * @subpackage Common
 * @author Jon Herron
 * @version 0.0.0.1
 * @since 02.24.2008
 */


require_once realpath(dirname(__file__) . "/../common/Environment.inc");
require_once "Config.php";
require_once "Log.php";

/**
 * Class used to aid with logging inside the Emailr codebase.
 *
 * This class will provide methods to aid with logging inside the Emailr codebase, serving as 
 * a wrapper around the Config and Log packages from PEAR.  Using these two applications together, 
 * we can get a nice configurable logging system pretty much out of the box.
 * @author Jon Herron
 * @package Emailr
 * @subpackage Common
 * @version 0.0.0.1
 * @since 02.24.2008
 */
class Logger
{
	/**
	 * Private data member to hold the composite logger.
	 * @access private
	 * @var $_composite
	 * @author Jon Herron
	 * @version 0.0.0.1
	 * @since 02.24.2008
	 */
	private $_composite;

	/**
	 * Public constructor for Logger
	 *
	 * This public constructor will create a new instance of the Logger class.
	 * @access public
	 * @author Jon Herron
	 * @returns DirectoryBrowser class instance
	 * @version 0.0.0.1
	 * @since 02.24.2008
	 */
	public function Logger()
	{
		// configure the logger using the Config package from PEAR
		$this->Configure();
	}

	/**
	 * Public function to log an alert message
	 *
	 * This public function will log the passed in message to which ever log types happen to be 
	 * configured at the given moment using the alert level
	 * @access public
	 * @author Jon Herron
	 * @param string $message the message to log
	 * @version 0.0.0.1
	 * @since 02.23.2008
	 */
	public function Alert($message)
	{
		// write the log message
		//$this->_composite->log($message, PEAR_LOG_ALERT);
		$this->_composite->alert($message);
	}

	/**
	 * Public function to log a critical message
	 *
	 * This public function will log the passed in message to which ever log types happen to be 
	 * configured at the given moment using the critical level
	 * @access public
	 * @author Jon Herron
	 * @param string $message the message to log
	 * @version 0.0.0.1
	 * @since 02.23.2008
	 */
	public function Critical($message)
	{
		// write the log message
		//$this->_composite->log($message, PEAR_LOG_CRIT);
		$this->_composite->crit($message);
	}

	/**
	 * Public function to log a debug message
	 *
	 * This public function will log the passed in message to which ever log types happen to be 
	 * configured at the given moment using the debug level
	 * @access public
	 * @author Jon Herron
	 * @param string $message the message to log
	 * @version 0.0.0.1
	 * @since 02.23.2008
	 */
	public function Debug($message)
	{
		// write the log message
		//$this->_composite->log($message, PEAR_LOG_DEBUG);
		$this->_composite->debug($message);
	}

	/**
	 * Public function to log an emergency message
	 *
	 * This public function will log the passed in message to which ever log types happen to be 
	 * configured at the given moment using the emergency level
	 * @access public
	 * @author Jon Herron
	 * @param string $message the message to log
	 * @version 0.0.0.1
	 * @since 02.23.2008
	 */
	public function Emergency($message)
	{
		// write the log message
		//$this->_composite->log($message, PEAR_LOG_EMERG);
		$this->_composite->emerg($message);
	}

	/**
	 * Public function to log a error message
	 *
	 * This public function will log the passed in message to which ever log types happen to be 
	 * configured at the given moment using the error level
	 * @access public
	 * @author Jon Herron
	 * @param string $message the message to log
	 * @version 0.0.0.1
	 * @since 02.23.2008
	 */
	public function Error($message)
	{
		// write the log message
		//$this->_composite->log($message, PEAR_LOG_ERR);
		$this->_composite->err($message);
	}

	/**
	 * Public function to log an info message
	 *
	 * This public function will log the passed in message to which ever log types happen to be 
	 * configured at the given moment using the info level
	 * @access public
	 * @author Jon Herron
	 * @param string $message the message to log
	 * @version 0.0.0.1
	 * @since 02.23.2008
	 */
	public function Info($message)
	{
		// write the log message
		//$this->_composite->log($message, PEAR_LOG_INFO);
		$this->_composite->info($message);
	}

	/**
	 * Public function to log a notice message
	 *
	 * This public function will log the passed in message to which ever log types happen to be 
	 * configured at the given moment using the notice level
	 * @access public
	 * @author Jon Herron
	 * @param string $message the message to log
	 * @version 0.0.0.1
	 * @since 02.23.2008
	 */
	public function Notice($message)
	{
		// write the log message
		//$this->_composite->log($message, PEAR_LOG_NOTICE);
		$this->_composite->notice($message);
	}

	/**
	 * Public function to log a warning message
	 *
	 * This public function will log the passed in message to which ever log types happen to be 
	 * configured at the given moment using the warning level
	 * @access public
	 * @author Jon Herron
	 * @param string $message the message to log
	 * @version 0.0.0.1
	 * @since 02.23.2008
	 */
	public function Warning($message)
	{
		// write the log message
		//$this->_composite->log($message, PEAR_LOG_WARNING);
		$this->_composite->warning($message);
	}

	/**
	 * Private function to configure the class instance
	 *
	 * This private function will use the Config package from PEAR to configure the Log package 
	 * from PEAR.  This will allow logging levels to be changed on a per system basis without 
	 * having to modify the code.
	 * @access private
	 * @author Jon Herron
	 * @version 0.0.0.1
	 * @since 02.24.2008
	 */
	private function Configure()
	{
		// create a new composite logger
		$this->_composite = &Log::singleton("composite");

		// read information about our database connection from the emailr.config file
		$config = new Config();
		$root =& $config->parseConfig(realpath(dirname(__file__) . "/../") . "/config/emailr.conf", "XML");

		$settings = $root->toArray();

		// 
		//	Fix for bug #1911591 - logger.inc does not work properly when only one logger is defined.  This 
		//	was due to invalid assumptions around the usage of $logger[$i]
		//
		$loggers = $settings["root"]["emailr"]["logging"];
		$multipleLoggers = (!(isset($loggers["logger"]["enabled"])));
		$loggersCount = (($multipleLoggers) ? count($loggers["logger"]) : 1);
		
		for($i = 0; $i < $loggersCount; $i++)
		{
			$enabled = (($multipleLoggers) ? $loggers["logger"][$i]["enabled"] : $loggers["logger"]["enabled"]);

			if($enabled == "true")
			{
				$minlevel = (($multipleLoggers) ? $loggers["logger"][$i]["minlevel"] : $loggers["logger"]["minlevel"]);
				$handler = (($multipleLoggers) ? $loggers["logger"][$i]["handler"] : $loggers["logger"]["handler"]);
				$name = (($multipleLoggers) ? $loggers["logger"][$i]["name"] : $loggers["logger"]["name"]);
				$identity = (($multipleLoggers) ? $loggers["logger"][$i]["identity"] : $loggers["logger"]["identity"]);
				$minLevel = (($multipleLoggers) ? $loggers["logger"][$i]["level"] : $loggers["logger"]["level"]);

				if($name != "")
				{
					$name = realpath(dirname(__file__) . "/../") . $name;
				}

				$logger = &Log::singleton($handler, $name, $identity);
				
				switch($minLevel)
				{
					case "debug":
						$mask = Log::MAX(PEAR_LOG_DEBUG);
						break;
					case "info":
						$mask = Log::MAX(PEAR_LOG_INFO);
						break;
					case "notice":
						$mask = Log::MAX(PEAR_LOG_NOTICE);
						break;
					case "warning":
						$mask = Log::MAX(PEAR_LOG_WARNING);
						break;
					case "err":
						$mask = Log::MAX(PEAR_LOG_ERR);
						break;
					case "crit":
						$mask = Log::MAX(PEAR_LOG_CRIT);
						break;
					case "alert":
						$mask = Log::MAX(PEAR_LOG_ALERT);
						break;
					case "emerg":
					default:
						$mask = Log::MAX(PEAR_LOG_EMERG);
						break;					
				}
				
				$logger->setMask($mask);
				
				$this->_composite->addChild($logger);
			}
		}

		// write to the log to indicate the log file was created
		$this->Info("Formed Singleton Composite Logger");
	}
}

?>
Return current item: Emailr