begin process at 2008 07 20 02:52:46
1 213 103 membres
26 nouveaux aujourd'hui
14 166 membres club

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 !

PHP 5 CLASSE DE LOG PARAMÉTRÉE PAR UN FICHIER XML


Information sur la source

Catégorie :Fichier / Disque Classé sous : log, fichier, xml Niveau : Débutant Date de création : 28/02/2007 Vu / téléchargé: 4 512 / 276

Note :
9 / 10 - par 1 personne
9,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (2)
Ajouter un commentaire et/ou une note

Description

Cette source est simplement une classe de logs paramétrée par un fichier xml : on peut y ajouter différents types de logs (debug, error, sql, etc...) définir un fichier différent pour chaque type et définir une adresse mail qui recevra la liste des erreurs.
Remarque : l'adresse mail est optionnelle (évite le spam dans un projet en cours de développement).

Cette source se décompose en 3 fichiers :
log.php -> contient la classe en elle même
logConfig.xml -> configuration
test.php -> un fichier de test que j'ai utilisé pour le développement

Source

  • <?php
  • /**
  • * Log manager class
  • *
  • */
  • class Log
  • {
  • /**
  • * Default log file
  • *
  • */
  • const DEFAULT_LOG_FILE = 'default.xml';
  • /**
  • * Xml file initialization
  • *
  • */
  • const INITIALIZATION_TEXT = "<?xml version='1.0' encoding='ISO-8859-1' standalone='yes'?><logs></logs>";
  • /**
  • * Default mail subject
  • *
  • */
  • const MAIL_SUBJECT = 'Error log';
  • /**
  • * log type
  • *
  • * @var string
  • */
  • private $type;
  • /**
  • * file name
  • *
  • * @var string
  • */
  • private $file;
  • /**
  • * mail address
  • *
  • * @var string
  • */
  • private $mail;
  • /**
  • * log message list
  • *
  • * @var array
  • */
  • private $logArray = array();
  • /**
  • * Accessor
  • *
  • * @param string $type
  • */
  • public function setType($type)
  • {
  • $this -> type = $type;
  • }
  • /**
  • * Accessor
  • *
  • * @return string
  • */
  • public function getType()
  • {
  • return $this -> type;
  • }
  • /**
  • * Accessor
  • *
  • * @param string $file
  • */
  • public function setFile($file)
  • {
  • $this -> file = $file;
  • }
  • /**
  • * Accessor
  • *
  • * @return string
  • */
  • public function getFile()
  • {
  • return $this -> file;
  • }
  • /**
  • * Accessor
  • *
  • * @return array
  • */
  • public function getLogArray()
  • {
  • return $this -> logArray;
  • }
  • /**
  • * Accessor
  • *
  • * @param array $array
  • */
  • public function setLogArray( array $array )
  • {
  • $this -> logArray = $array;
  • }
  • /**
  • * Accessor
  • *
  • * @return string
  • */
  • public function getMail()
  • {
  • return $this -> mail;
  • }
  • /**
  • * Accessor
  • *
  • * @param string $mail
  • */
  • public function setMail($mail)
  • {
  • $this -> mail = $mail;
  • }
  • /**
  • * Constructor
  • *
  • * @param string $type
  • */
  • public function __construct($type = 'udefined')
  • {
  • $this -> type = $type;
  • //get log file from xml config
  • $xml = simplexml_load_file('logConfig.xml');
  • $xpathResult = (array) $xml -> xpath("/logs/log[type='" . $type . "']/file");
  • $file = (string) $xpathResult[0];
  • if (trim($file) !='')
  • $this -> file = $file;
  • else
  • $this -> file = self::DEFAULT_LOG_FILE;
  • //get mail from xml config
  • $xpathResult = (array) $xml -> xpath("/logs/log[type='" . $type . "']/mail");
  • $mail = (string) $xpathResult[0];
  • if (trim($mail) !='')
  • $this -> mail = $mail;
  • }
  • /**
  • * Write a log message in xml file
  • *
  • * @param string $logMessage
  • */
  • public function write($logMessage)
  • {
  • if (!file_exists($this -> file) )
  • {
  • //if not exists -> creat
  • $file = fopen($this -> file, 'w');
  • fputs($file, self::INITIALIZATION_TEXT);
  • fclose($file);
  • }
  • //xml building
  • $xml = simplexml_load_file($this -> file);
  • $xml->addChild('log');
  • $newNodeIndex = count($xml -> log) - 1;
  • $xml-> log[$newNodeIndex] ->addChild('type', $this -> type);
  • $xml-> log[$newNodeIndex] ->addChild('date', date('d-m-Y H:i:s'));
  • $xml-> log[$newNodeIndex] -> addChild('message', $logMessage);
  • //log array building
  • $this -> logArray[] = date('d-m-Y H:i:s') . " " . $this -> type . " : " . $logMessage;
  • //write xml in file
  • $handle = fopen($this -> file, 'w+');
  • fputs($handle, $xml->asXML());
  • fclose($handle);
  • }
  • /**
  • * Send a mail : see logConfig.xml
  • * only if mail is set in xml file
  • *
  • */
  • public function sendMail()
  • {
  • if( isset($this -> mail))
  • {
  • $mailText = '';
  • foreach ($this -> logArray as $log)
  • $mailText .= $log . "\n";
  • if( trim($mailText)!='' )
  • mail($this -> mail, self::MAIL_SUBJECT, $mailText);
  • }
  • }
  • /**
  • * Desctructor
  • *
  • */
  • public function __destruct()
  • {
  • $this -> sendMail();
  • }
  • }
  • ?>
<?php
/**
 * Log manager class
 *
 */
class Log
{

    /**
     * Default log file
     *
     */
    const DEFAULT_LOG_FILE = 'default.xml';

    /**
     * Xml file initialization
     *
     */
    const INITIALIZATION_TEXT = "<?xml version='1.0' encoding='ISO-8859-1' standalone='yes'?><logs></logs>";

    /**
     * Default mail subject
     *
     */
    const MAIL_SUBJECT = 'Error log';


    /**
     * log type
     *
     * @var string
     */
    private $type;

    /**
     * file name
     *
     * @var string
     */
    private $file;

    /**
     * mail address
     *
     * @var string
     */
    private $mail;

    /**
     * log message list
     *
     * @var array
     */
    private $logArray = array();


    /**
     * Accessor
     *
     * @param string $type
     */
    public function setType($type)
    {
        $this -> type = $type;
    }

    /**
     * Accessor
     *
     * @return string
     */
    public function getType()
    {
        return $this -> type;
    }

    /**
     * Accessor
     *
     * @param string $file
     */
    public function setFile($file)
    {
        $this -> file = $file;
    }

    /**
     * Accessor
     *
     * @return string
     */
    public function getFile()
    {
        return $this -> file;
    }

    /**
     * Accessor
     *
     * @return array
     */
    public function getLogArray()
    {
        return $this -> logArray;
    }

    /**
     * Accessor
     *
     * @param array $array
     */
    public function setLogArray( array $array )
    {
        $this -> logArray = $array;
    }

    /**
     * Accessor
     *
     * @return string
     */
    public function getMail()
    {
        return $this -> mail;
    }

    /**
     * Accessor
     *
     * @param string $mail
     */
    public function setMail($mail)
    {
       $this -> mail = $mail;
    }

    /**
     * Constructor
     *
     * @param string $type
     */
    public function __construct($type = 'udefined')
    {
        $this -> type = $type;

        //get log file from xml config
        $xml = simplexml_load_file('logConfig.xml');
        $xpathResult = (array) $xml -> xpath("/logs/log[type='" . $type . "']/file");
        $file = (string) $xpathResult[0];

        if (trim($file) !='')
            $this -> file = $file;
        else
            $this -> file = self::DEFAULT_LOG_FILE;

        //get mail from xml config
        $xpathResult = (array) $xml -> xpath("/logs/log[type='" . $type . "']/mail");
        $mail = (string) $xpathResult[0];

        if (trim($mail) !='')
            $this -> mail = $mail;

    }

    /**
     * Write a log message in xml file
     *
     * @param string $logMessage
     */
    public function write($logMessage)
    {
        if (!file_exists($this -> file) )
        {
            //if not exists -> creat
            $file = fopen($this -> file, 'w');
            fputs($file, self::INITIALIZATION_TEXT);
            fclose($file);
        }

        //xml building
        $xml = simplexml_load_file($this -> file);
        $xml->addChild('log');
        $newNodeIndex = count($xml -> log) - 1;
        $xml-> log[$newNodeIndex] ->addChild('type', $this -> type);
        $xml-> log[$newNodeIndex] ->addChild('date', date('d-m-Y H:i:s'));
        $xml-> log[$newNodeIndex] -> addChild('message', $logMessage);

        //log array building
        $this -> logArray[] = date('d-m-Y H:i:s') . " " . $this -> type . " : " . $logMessage;

        //write xml in file
        $handle = fopen($this -> file, 'w+');
        fputs($handle, $xml->asXML());
        fclose($handle);
    }

    /**
     * Send a mail : see logConfig.xml
     * only if mail is set in xml file
     *
     */
    public function sendMail()
    {
        if( isset($this -> mail))
        {

            $mailText = '';

            foreach ($this -> logArray as $log)
                $mailText .= $log . "\n";

            if( trim($mailText)!='' )
                mail($this -> mail, self::MAIL_SUBJECT, $mailText);
        }
    }


    /**
     * Desctructor
     *
     */
    public function __destruct()
    {
        $this -> sendMail();
    }


}
?>
Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip

  • signaler à un administrateur
    Commentaire de morpheus57 le 02/03/2007 19:12:54

    Ah oui, j'ai oublié de préciser :
    Il est possible d'ajouter un nouveau type d'erreur simplement en modifiant le fichier logConfig.xml.

    Ensuite le nouveau type de log peu être directement instancié.

  • signaler à un administrateur
    Commentaire de oox le 22/04/2007 12:56:20

    Petite erreur dans ton code :
    if (isset($this -> mail))

    Cette ligne retournera toujours true.
    => if (! empty($this -> mail))

    Sinon rien à redire, bon code.

Ajouter un commentaire

Pub



Appels d'offres

Dessins techniques
Budget : 60€
Animation Flash - Doma...
Budget : 370€
Application flash medi...
Budget : 1 000€

CalendriCode

Juillet 2008
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

VS Express FR Gratuit !

VS Express en français et 100% gratuit !

Téléchargements

Logiciels à télécharger sur le même thème :

Boutique

Boutique de goodies CodeS-SourceS