|
Trouver une ressource
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
CLASS DE LOG PHP5
Information sur la source
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
- <?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());
- }
- ?>
<?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
Historique
- 04 décembre 2007 17:14:18 :
- maj
- 04 décembre 2007 17:47:32 :
- MAJ
- 04 décembre 2007 19:41:16 :
- MAJ : ajout d'une class factory
- 04 décembre 2007 19:42:32 :
- MAJ : mise a jour du zip
- 04 décembre 2007 23:20:17 :
- MAJ : rajout <?php ?>
Sources du même auteur
Sources de la même categorie
Sources en rapport avec celle ci
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
template.inc [ par joedalton ]
bonjourVoila, je possede un fichier template.inc qui me permet de définir ma classe template, mais le probleme c'est que ce fichier est valable pour l
pb avec ma classe [ par windu ]
voila, j'utilise 1 classe de connexion (MySQL), classe que j'appelle dasn mon fichier de connexion à la base. tout va bien il me suffit d'inclure ce f
Affichage de graphique [ par nbenoist ]
sur RedHat 7.2 J'ai une page php qui lit un fichier de log. Ma page php me affiche 2 camenbert suivant les informations du fichier log. J'ai mis les
une classe pour toute la navigation [ par dark_naruto25 ]
Salut Je suis en train de faire une classe dans un fichier que j'inclue dans tout les fichier qui en ont besoin. Seulement je veux créer une seul
extraction de donnee d'un fichier .log [ par Noiprocs77 ]
Bonjours a toutes et tous .Voila apres des jours de recherche sur le net , je me decide a poster en esperant qu'on puisse me repondre favorablement .D
Problème de portée de variable URGENT [ par Tupac59 ]
Voila le problème: depuis la page précédente je POST ['log2'], dans la page suivante je récupère ce log2 dans la variable $log et cela fonctionne puis
Verification de fichier [ par godofgames ]
Bonjour j'ai une question j'ai un site a realiser pour quelqu'un et cette personne veux pouvoir faire les modification elle meme mais elle ne veux rie
un fichier de fonctions ou une classe de fonctions ? [ par mickaelpfr ]
Bonjour a tous , dans le cadre du dèveloppement d'un projet , j'ai de nombreuses fonction n'ayant pas de rapport entre elle mais je me demandais s'il
Modifier un fichier .log en profondeur [ par Equilibrius ]
Bonjour a tous, voilà j'ai un petit problemme, j'ai un fichier .log qui fait plus de 2.5Mo (avec en gros 40'000 lignes) il contien les log de plusieur
php - intranet - MysqL [ par ben272 ]
Bonsoir à tous, j'ai quelques questions à poser. Voilà dans mon collège on va mettre en place un intranet ( section informatique seulement ),et c'est
|
Téléchargements
Logiciels à télécharger sur le même thème :
|