5/5 (11 avis)
Vue 6 133 fois - Téléchargée 286 fois
<?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; } } ?>
4 déc. 2007 à 22:20
je suis nommé dans le descriptif, alors que puis-je dire... ;-)
Nan, sérieusement, c'est très correctement codé.
Ca mérite que j'approfondisse en testant le code. J'ai pas franchement le temps, là, mais je tâcherai de le faire (sans garntie hein).
Juste : j'aurais lancé des exceptions moi, en testant les fonctions dont tu masques les erreurs avec un @. Mais j'ai juste parcouru le code donc je ne m'avance pas trop, si ça se trouve t'as une gestion d'erreur externe. Si ce n'est pas le cas : gère ces erreurs. Un fopen() qui échoue a généralement de grosses conséquences.
Bien en tous cas, très bonne qualité de code :-) Et sujet intéressant (logger, bordel, c'est vraiment vital quand on développe des applicatifs web!!)
4 déc. 2007 à 22:20
4 déc. 2007 à 22:52
4 déc. 2007 à 23:26
Pour les exceptions elles sont géré dans une class externe
ExceptionFactory et ExceptionLog fournies avec le zip.
Ex du fopen
if( false ($this->ressource $this->_open()) )
{
ExceptionFactory::catchException(ExceptionLogs::ERR_MESS_OPEN,
ExceptionLogs::ERR_CODE_OPEN,
__CLASS__, __FUNCTION__, $this->options);
return false;
}
4 déc. 2007 à 23:32
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.