Class de log php5

Description

Bonjour tous le monde,
Voila j'ai créé une classe permettant de gérer les logs avec plusieurs sorties possibles. Pour l'instant j'ai créé que la sortie vers un fichier mais on peut facilement y rajouter d'autre type de sortie (xml, bdd etc...)
Je me suis inspiré de plusieurs framework et source (notement celle de malalam et fhx) que j'ai mis a ma sauce.
J'attend vaut retour afin de l'améliorer.

Source / Exemple :


<?php

class LogsFactory
{
	private $options	= array('EXCEPTION_ON_ERREUR' 	=> true, 
					'EXCEPTION_ON_FILE' 	=> false);
	
	public static function selectLogs($type, $param)
	{
		switch($type)
		{
			case 'file':
				$log 	= System::getModule('LogsToFile', $param);
				break;
			case 'xml':
				$log 	= System::getModule('LogsToXml', $param);
				break;
			case 'bdd':
				$log 	= System::getModule('LogsToBdd', $param);
				break;
			default:
				ExceptionFactory::catchException(ExceptionLogs::ERR_MESS_TYPE, 
						     ExceptionLogs::ERR_CODE_TYPE, 
						     __CLASS__, __FUNCTION__, $this->options);
				break;
		}
		
		return $log;
	}
}

?>

<?php
abstract class Logs
{
	private $options = array('EXCEPTION_ON_ERREUR' 	=> true, 
				 'EXCEPTION_ON_FILE' 	=> false);
	
	protected $contenuLog	= array('NAME_LEVEL'	=> '',
					'NUM_LEVEL'	=> '',
					'DATE_LOG'	=> '',
					'MESSAGE'	=> '');
	
	protected $formatage;
	protected $ressource;
	
	public function open()
	{
		if( false === ($this->ressource = $this->_open()) )
		{
			ExceptionFactory::catchException(ExceptionLogs::ERR_MESS_OPEN, 
						         ExceptionLogs::ERR_CODE_OPEN, 
							 __CLASS__, __FUNCTION__, $this->options);
											 
			return false;
		}
		
		return true;
	}
	
	public function write()
	{
		if( false === $this->_write($this->formatLog()) )
		{
			ExceptionFactory::catchException(ExceptionLogs::ERR_MESS_WRITE, 
							 ExceptionLogs::ERR_CODE_WRITE, 
							 __CLASS__, __FUNCTION__, $this->options);
											 
			return false;
		}
		
		return true;
	}
	
	public function read()
	{
		if( false === $this->_read() )
		{
			ExceptionFactory::catchException(ExceptionLogs::ERR_MESS_READ, 
							 ExceptionLogs::ERR_CODE_READ, 
							__CLASS__, __FUNCTION__, $this->options);
						
			return false;
		}
		
		return true;
	}
	
	protected function getLevel($lvl)
	{
		$level = System::getModule('LogsLevel');
		$level = $level->getErrorLevel($lvl);
		
		return $level;
	}
	
	private function formatLog()
	{
		return $this->formatage->replaceFormat($this->contenuLog);
	}
	
	abstract public function _open();
	abstract public function _write($contenu);
	abstract public function _read();
}
?>

<?php
class LogsToFile extends Logs
{
	private $url;
	
	public function __construct($param)
	{
		if( is_resource($param['RESSOURCE']) )
		{
			if ( get_resource_type($param['RESSOURCE']) != 'stream' ) 
			{
                             ExceptionFactory::catchException(ExceptionLogs::ERR_MESS_RESSOURCE, 
							 ExceptionLogs::ERR_CODE_RESSOURCE, 
							 __CLASS__, __FUNCTION__, $this->options);
                        }
             
                        $this->ressource = $param['RESSOURCE'];
		}
		else
		{
			$this->url 	= $param['RESSOURCE'];
			$this->open();
		}
		
		$this->formatage			= System::getModule('FormatText');
		$this->contenuLog['NAME_LEVEL'] 	= $this->getLevel($param['LEVEL']);
		$this->contenuLog['NUM_LEVEL']		= $param['LEVEL'];
		$this->contenuLog['DATE_LOG']		= date("d-m-Y H:i:s");
		$this->contenuLog['MESSAGE']		= $param['MESSAGE'];
	}
	
	public function _open()
	{
		return @fopen($this->url, 'a+');
	}
	
	public function _write($contenu)
	{		
		return @fwrite($this->ressource, $contenu);
	}
	
	public function _read()
	{
		if( filesize($this->url) != 0 )
		{
			return @fread($this->ressource, filesize($this->url));
		}
		else
		{
			return NULL;
		}
	}
	
	public function close()
	{
		if( is_resource($this->ressource) )
		{
			@fclose($this->ressource);
		}
	}
}
?>

<?php
class LogsLevel
{
	private $options	= array('EXCEPTION_ON_ERREUR' 	=> true, 
					'EXCEPTION_ON_FILE' 	=> false);
	
	private $errorLevel	= array('0' => 'GOOD',
					'1' => 'WARNING', 
					'2' => 'ERROR', 
					'3' => 'FATAL_ERROR');
	
	public function getErrorLevel($numLevel)
	{
		if( $numLevel < 0 && $numLevel > 3 )
		{
			ExceptionFactory::catchException(ExceptionLogs::ERR_MESS_LEVEL, 
							 ExceptionLogs::ERR_CODE_LEVEL, 
							 __CLASS__, __FUNCTION__, $this->options);
		}
		
		if(! is_integer($numLevel) )
		{
			ExceptionFactory::catchException(ExceptionLogs::ERR_MESS_INTEGER, 
					                 ExceptionLogs::ERR_CODE_INTEGER, 
							 __CLASS__, __FUNCTION__, $this->options);
		}
		
		return $this->errorLevel[$numLevel];
	}
}
?>

<?php
class FormatText
{
	private $formatage;

    public function __construct()
    {
        $format = "{NAME_LEVEL} ({NUM_LEVEL}) : {DATE_LOG} : {MESSAGE}\n";
        $this->formatage = $format;
    }
    
    public function replaceFormat($log)
    {
    	$out = $this->formatage;
    	
        foreach ($log as $name => $value) 
        {
            $out = str_replace('{'.$name.'}', $value, $out);
        }
        
        return $out;
    }
}
?>

                                  • Uitlisation ********************
<?php require_once('./ressources/autoload.php'); try { $param = array('RESSOURCE' => './test.txt', 'LEVEL' => 1, 'MESSAGE' => 'Erreur test test test 2'); $log = LogsFactory::selectLogs('file', $param); $log->write(); } catch(Exception $error) { die($error->getMessage()); } catch(ExceptionLogs $error) { die($error->myGetMessage()); } ?>

Conclusion :


N'hesitez pas a donner votre avis

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.