begin process at 2012 05 27 17:57:32
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Class et Objet ( POO )

 > [PHP5] CLASSE DE LOG

[PHP5] CLASSE DE LOG


 Information sur la source

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

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Class et Objet ( POO ) Classé sous :classe, log, trace, requêtes, xml Niveau :Débutant Date de création :20/04/2006 Vu / téléchargé :3 749 / 320

Auteur : malalam

Ecrire un message privé
Site perso
Commentaire sur cette source (6)
Ajouter un commentaire et/ou une note

 Description

Ceci est une classe de log.
La classe en elle-même se trouve dans le répertoire class/class.oLogger.php

Les 2 fichiers log.consult.php et log.detail.php sont là pour un exemple d'utilisation.
Je les ai extirpé de ma propre interface...donc c'est du bidouillage pour que ça tourne en local lol :-) A vous de bidouiller la css fournie.
Mais vous avez déjà une bonne base quand même, un bon exemple d'utilisation des logs créés.

Les logs sont déjà créés évidemment.

Pour créer les logs, personnellement, je les ai mis dans ma classe DB, dans la méthode effectuant une requête (oDB::query). Je ne logge pas les SELECT, mais ça, c'est un choix :-)
Donc, je lance mon log ainsi :

if (true === $log && substr (strtoupper (trim ($this->s_db_sql)), 0, 6) !== 'SELECT') {
$ologger = new oLogger;
$destId = (isset ($_SESSION['dest_id']))?$_SESSION['dest_id']:0;
$o logger -> init ($this -> s_db_sql, $destId);
}

C'est tout! C'est ce petit bout de code qui a généré les quelques logs que je montre ici.

Source

  • <?php
  • /**
  • * class oLogger
  • * @author : johan <barbier_johan@hotmail.com>
  • * @version : 2006-04-20
  • */
  • class oLogger {
  • /**
  • * const sLogDir
  • * path to the logs
  • */
  • const sLogDir ='logs';
  • /**
  • * private array aLogs
  • * array to store each log line
  • */
  • private $aLogs = array (
  • 'PAGE' => '',
  • 'DATE' => '',
  • 'QUERY' => '',
  • 'TYPE' => ''
  • );
  • /**
  • * private array aLevels
  • * array to store the query types
  • */
  • private $aLevels = array (
  • 0 => 'UNKNOWN',
  • 1 => 'SELECT',
  • 2 => 'UPDATE',
  • 3 => 'INSERT',
  • 4 => 'DELETE'
  • );
  • /**
  • * public array aDir
  • * array to store all the log folders and log files
  • */
  • public $aDir = array ();
  • /**
  • * private object oXml
  • * a simple_xml object
  • */
  • private $oXml = null;
  • /**
  • * public function __construct
  • * the constructor
  • * initailizes the simple_xml object if needed
  • * @param (string) file : the xml log file from which will be set the simple_xml object property
  • */
  • public function __construct ($file = '') {
  • if (!empty ($file)) {
  • $this -> oXml = simplexml_load_file(self::sLogDir.'/'.$file);
  • }
  • }
  • /**
  • * public function init
  • * initializes the logger
  • * gets the sql query, the user id, current date, filename, query type, and builds the aLogs array with all of that
  • * @param (string) sSql : the sql query we want to log
  • * @param (int) destId : the user id (depends on your system)
  • */
  • public function init ($sSql, $destId) {
  • $page = $_SERVER['PHP_SELF'];
  • $this -> checkDir ($destId);
  • $date = date ('H:i:s');
  • $file = date ('Ymd').'.xml';
  • $sSql = trim (preg_replace ('@(\s)+@', ' ', $sSql));
  • $aSql = explode (' ', $sSql);
  • if (false === ($level = array_search ($aSql[0], $this -> aLevels))) {
  • $level = 0;
  • }
  • $this -> aLogs = array (
  • 'PAGE' => $page,
  • 'DATE' => $date,
  • 'QUERY' => $sSql,
  • 'TYPE' => $this -> aLevels[$level]
  • );
  • $this -> logIt ($file, $destId);
  • }
  • /**
  • * private function checkDir
  • * used to check if a log folder exists for a given user id. If not, creates it.
  • * @param (int) id : the user id
  • */
  • private function checkDir ($id) {
  • if (!file_exists (self::sLogDir.'/'.$id)) {
  • mkdir (self::sLogDir.'/'.$id);
  • }
  • }
  • /**
  • * private function logIt
  • * build the xml from the aLogs array, and saves it into the given file
  • * if the given file already exists, it appends the new xml to the existing one
  • * @param (string) file : the filename in which the xml log will be written
  • * @param (int) destId : the user id (depends on your system)
  • */
  • private function logIt ($file, $destId) {
  • if (file_exists (self::sLogDir.'/'.$destId.'/'.$file)) {
  • $docXml = DOMDocument::load (self::sLogDir.'/'.$destId.'/'.$file);
  • $root = $docXml -> documentElement;
  • } else {
  • $docXml = new DOMDocument('1.0', 'iso-8859-1');
  • $root = $docXml -> createElement ('logs');
  • $root = $docXml -> appendChild ($root);
  • }
  • $log = $docXml -> createElement ('log');
  • $log = $root -> appendChild ($log);
  • foreach ($this -> aLogs as $clef => $val) {
  • $xmlClef = $docXml -> createElement ($clef);
  • $xmlClef = $log -> appendChild ($xmlClef);
  • $xmlVal = $docXml -> createTextNode ($val);
  • $xmlVal = $xmlClef -> appendChild ($xmlVal);
  • }
  • $docXml -> save (self::sLogDir.'/'.$destId.'/'.$file);
  • $this -> aLogs = array ();
  • }
  • /**
  • * public function getaDir
  • * builds the aDir array, in which are stored all the log folders and log files
  • * @param (bool) ordID : if set to true, orders the array by users id ASC
  • */
  • public function getaDir ($ordID = false) {
  • $aDestLogs = scandir (self::sLogDir);
  • $aDestLogs = array_splice ($aDestLogs, 2);
  • if (true === $ordID) {
  • sort ($aDestLogs, SORT_NUMERIC);
  • }
  • if (!empty ($aDestLogs)) {
  • foreach ($aDestLogs as $v) {
  • $aFiles = scandir (self::sLogDir.'/'.$v);
  • $aFiles = array_splice ($aFiles, 2);
  • $this -> aDir[$v] = $aFiles;
  • }
  • } else {
  • $this -> aDir = array ();
  • }
  • }
  • /**
  • * public function getList
  • * builds an array from the oXml simple_xml object with all the values it contains in the $type xml element (the available types are the aLogs array keys)
  • * used for the logs UI : with this method, I can display only the UPDATE, or the INSERT, or only a given page on the logged website...
  • * @param (string) type : the type on wihch we want to filter. Must be a valid aLogs key.
  • */
  • public function getList ($type) {
  • if (!array_key_exists ($type, $this -> aLogs)) {
  • trigger_error ('Type '.$type.' non reconnu', E_USER_ERROR);
  • }
  • $oTypes = $this -> oXml -> xpath ('log/'.$type);
  • $aType = array_unique ($oTypes);
  • return $aType;
  • }
  • /**
  • * public function getDetail
  • * transforms the xml in a log file to html. Could unfortunately not use the XSLTProcessor, because my server did not implement it :-(
  • * the html can, be changed, of course. The javascript is a javascript function used to get a filter, if needed.
  • * @param (bool) ord : if set to true, we reverse the xml; as the xml is logged chronologically, we will reverse the dates.
  • * @param (string) type : if not empty, we filter on that type.
  • * @param (string) page : if not empty, we filter on that page.
  • */
  • public function getDetail ($ord = false, $type = '', $page = '') {
  • $sXml = '';
  • $oLignes = $this -> oXml -> xpath ('log');
  • if (true === $ord) {
  • $oLignes = array_reverse ($oLignes);
  • }
  • $iCpt = 0;
  • foreach ($oLignes as $log) {
  • if (!empty ($type) && $log -> TYPE != $type) {
  • continue;
  • }
  • if (!empty ($page) && $log -> PAGE != $page) {
  • continue;
  • }
  • $sColor = ($iCpt%2) ? 'ct1' : 'ct2';
  • $sXml .= <<<HTML
  • <tr>
  • <td class="$sColor">{$log -> DATE}</td>
  • <td class="$sColor"><a href="javascript:filtrePage('{$log -> PAGE}');">{$log -> PAGE}</a></td>
  • <td class="$sColor"><a href="javascript:filtreType('{$log -> TYPE}');">{$log -> TYPE}</a></td>
  • <td class="$sColor">{$log -> QUERY}</td>
  • </tr>
  • HTML;
  • $iCpt ++;
  • }
  • return $sXml;
  • }
  • }
  • ?>
<?php
/**
* class oLogger
* @author : johan <barbier_johan@hotmail.com>
* @version : 2006-04-20
*/
class oLogger {

	/**
	* const sLogDir
	* path to the logs
	*/
	const sLogDir ='logs';

	/**
	* private array aLogs
	* array to store each log line
	*/
	private $aLogs = array (
			'PAGE' => '',
			'DATE' => '',
			'QUERY' => '',
			'TYPE' => ''
			);

	/**
	* private array aLevels
	* array to store the query types
	*/
	private $aLevels = array (
		0 => 'UNKNOWN',
		1 => 'SELECT',
		2 => 'UPDATE',
		3 => 'INSERT',
		4 => 'DELETE'
		);

	/**
	* public array aDir
	* array to store all the log folders and log files
	*/
	public $aDir = array ();

	/**
	* private object oXml
	* a simple_xml object
	*/
	private $oXml = null;

	/**
	* public function __construct
	* the constructor
	* initailizes the simple_xml object if needed
	* @param (string) file : the xml log file from which will be set the simple_xml object property
	*/
	public function __construct ($file = '') {
		if (!empty ($file)) {
			$this -> oXml = simplexml_load_file(self::sLogDir.'/'.$file);
		}
	}

	/**
	* public function init
	* initializes the logger
	* gets the sql query, the user id, current date, filename, query type, and builds the aLogs array with all of that
	* @param (string) sSql : the sql query we want to log
	* @param (int) destId : the user id (depends on your system)
	*/
	public function init ($sSql, $destId) {
		$page = $_SERVER['PHP_SELF'];
		$this -> checkDir ($destId);
		$date = date ('H:i:s');
		$file = date ('Ymd').'.xml';
		$sSql = trim (preg_replace ('@(\s)+@', ' ', $sSql));
		$aSql = explode (' ', $sSql);
		if (false === ($level = array_search ($aSql[0], $this -> aLevels))) {
			$level = 0;
		}
		$this -> aLogs = array (
			'PAGE' => $page,
			'DATE' => $date,
			'QUERY' => $sSql,
			'TYPE' => $this -> aLevels[$level]
			);
		$this -> logIt ($file, $destId);
	}

	/**
	* private function checkDir
	* used to check if a log folder exists for a given user id. If not, creates it.
	* @param (int) id : the user id
	*/
	private function checkDir ($id) {
		if (!file_exists (self::sLogDir.'/'.$id)) {
			mkdir (self::sLogDir.'/'.$id);
		}
	}

	/**
	* private function logIt
	* build the xml from the aLogs array, and saves it into the given file
	* if the given file already exists, it appends the new xml to the existing one
	* @param (string) file : the filename in which the xml log will be written
	* @param (int) destId : the user id (depends on your system)
	*/
	private function logIt ($file, $destId) {
		if (file_exists (self::sLogDir.'/'.$destId.'/'.$file)) {
			$docXml = DOMDocument::load (self::sLogDir.'/'.$destId.'/'.$file);
			$root = $docXml -> documentElement;
		} else {
			$docXml = new DOMDocument('1.0', 'iso-8859-1');
			$root = $docXml -> createElement ('logs');
			$root = $docXml -> appendChild ($root);
		}
		$log = $docXml -> createElement ('log');
		$log = $root -> appendChild ($log);
		foreach ($this -> aLogs as $clef => $val) {
			$xmlClef = $docXml -> createElement ($clef);
			$xmlClef = $log -> appendChild ($xmlClef);
			$xmlVal = $docXml -> createTextNode ($val);
			$xmlVal = $xmlClef -> appendChild ($xmlVal);
		}
		$docXml -> save (self::sLogDir.'/'.$destId.'/'.$file);
		$this -> aLogs = array ();
	}

	/**
	* public function getaDir
	* builds the aDir array, in which are stored all the log folders and log files
	* @param (bool) ordID : if set to true, orders the array by users id ASC
	*/
	public function getaDir ($ordID = false) {
		$aDestLogs = scandir (self::sLogDir);
		$aDestLogs = array_splice ($aDestLogs, 2);
		if (true === $ordID) {
			sort ($aDestLogs, SORT_NUMERIC);
		}
		if (!empty ($aDestLogs)) {
			foreach ($aDestLogs as $v) {
				$aFiles = scandir (self::sLogDir.'/'.$v);
				$aFiles = array_splice ($aFiles, 2);
				$this -> aDir[$v] = $aFiles;
			}
		} else {
			$this -> aDir = array ();
		}
	}

	/**
	* public function getList
	* builds an array from the oXml simple_xml object with all the values it contains in the $type xml element (the available types are the aLogs array keys)
	* used for the logs UI : with this method, I can display only the UPDATE, or the INSERT, or only a given page on the logged website...
	* @param (string) type : the type on wihch we want to filter. Must be a valid aLogs key.
	*/
	public function getList ($type) {
		if (!array_key_exists ($type, $this -> aLogs)) {
			trigger_error ('Type '.$type.' non reconnu', E_USER_ERROR);
		}
		$oTypes = $this -> oXml -> xpath ('log/'.$type);
		$aType = array_unique ($oTypes);
		return $aType;
	}

	/**
	* public function getDetail
	* transforms the xml in a log file to html. Could unfortunately not use the XSLTProcessor, because my server did not implement it :-(
	* the html can, be changed, of course. The javascript is a javascript function used to get a filter, if needed.
	* @param (bool) ord : if set to true, we reverse the xml; as the xml is logged chronologically, we will reverse the dates.
	* @param (string) type : if not empty, we filter on that type.
	* @param (string) page : if not empty, we filter on that page.
	*/
	public function getDetail ($ord = false, $type = '', $page = '') {
		$sXml = '';
		$oLignes = $this -> oXml -> xpath ('log');
		if (true === $ord) {
			$oLignes = array_reverse ($oLignes);
		}
		$iCpt = 0;
		foreach ($oLignes as $log) {
			if (!empty ($type) && $log -> TYPE != $type) {
				continue;
			}
			if (!empty ($page) && $log -> PAGE != $page) {
				continue;
			}
			$sColor = ($iCpt%2) ? 'ct1' : 'ct2';
			$sXml .= <<<HTML
			<tr>
				<td class="$sColor">{$log -> DATE}</td>
				<td class="$sColor"><a href="javascript:filtrePage('{$log -> PAGE}');">{$log -> PAGE}</a></td>
				<td class="$sColor"><a href="javascript:filtreType('{$log -> TYPE}');">{$log -> TYPE}</a></td>
				<td class="$sColor">{$log -> QUERY}</td>
			</tr>
HTML;
		$iCpt ++;
		}
		return $sXml;
	}
}

?>


 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Sources du même auteur

Source avec Zip ASTUCES/HACK PHP
SQUELETTE DE GESTION DES DROITS
[PHP 5.1] CLASS STRING : NOUVEL EXEMPLE SUR LA SPL
Source avec Zip Source avec une capture [PHP 5.1] PHOTOPHOP (PHPDRAW 2)
Source avec Zip Source avec une capture [PHP5.1] O-LOC : CLASSE ET BACKOFFICE D'INTERNATIONALISATION

 Sources de la même categorie

Source avec Zip GÉNÉRATION AUTOMATIQUE DE FICHIER .CLASS.PHP EN FONCTION D'U... par ig3
CLASSE D'OBJET DE CRYPTAGE ET DÉCRYPTAGE DE CHAINES DE CARAC... par 8Tnerolf8
Source avec Zip MY.DEVIANTART API par inwebo
CLASSE DE GESTION DE "VARIABLES GLOBALES D'ENVIRONNEMENT" par pifou25
Source avec Zip COLLECTION.CLASS.MIN.PHP par thunderhunter

 Sources en rapport avec celle ci

Source avec Zip CLASS DE LOG PHP5 par dorian91
TRACE DES ERREURS EN PHP par Laurpierre
Source avec Zip PHP 5 CLASSE DE LOG PARAMÉTRÉE PAR UN FICHIER XML par morpheus57
Source avec Zip [PHP5/DOM] CLASSE DE CREATION DE FLUX RSS VIA UN FICHIER XML par jean84
CLASSE SIMPLE DE CRÉATION DE FICHIER XML par pastis51forever

Commentaires et avis

Commentaire de FhX le 22/04/2006 19:02:26

Moi ce qui me gène, c'est que j'aurais bien vu ta classe de base qui fait un logging de base via les variables, et les classes qui étendent le tout niveau html.
Genre un truc comme ca :

Class oLogger {}
Class oLogToXml {}
Class oLogToFile {}
Class oLogToHtml {}
Class oLogToDatabase {}

Quelque chose comme ca... C'est plus joli moi jtrouve :)
Comme ca, dans ta classe de BDD par exemple, imaginons ca :

if (true === $log && substr (strtoupper (trim ($this->s_db_sql)), 0, 6) !== 'SELECT') {
if ( $debug === 'xml' ) $ologged = new oLogToXml;

$destId = (isset ($_SESSION['dest_id']))?$_SESSION['dest_id']:0;
$ologger -> init ($this -> s_db_sql, $destId);

$ologger->write_to_log(); // Qui sera une méthode commune à toutes les classes.
}

Quelque chose comme ca.
Tfacon, moi jdis ca, c'est que j'aime pas voir du HTML ou du XML ou n'importe quoi qui fasse de la mise en forme en sortie dans une classe :p

Commentaire de coucou747 le 24/04/2006 09:10:20 administrateur CS

     private $aLevels = array (
         0 => 'UNKNOWN',
         1 => 'SELECT',
         2 => 'UPDATE',
         3 => 'INSERT',
         4 => 'DELETE'
         );
  

pourquoi ne pas mettre :

private $aLevels = array (
'UNKNOWN',
'SELECT',
'UPDATE',
'INSERT',
'DELETE'
);

?

Commentaire de malalam le 24/04/2006 10:08:31 administrateur CS

FhX => je suis d'accord avec toi, j'aurais dû séparer. De même pour la mise en forme...mais là je suis moyennement d'accord lol. Disons que j'aurais dû faire une classe séparée, mais cela ne me dérange pas de faire une classe qui crée une mise en forme.
Au passage, le xml n'est PAS de la mise en forme...le xsl, oui... :-)
Si je ne l'ai pas fait, c'est parce que j'ai extirpé cette classe de mon projet au taf, et qu'on a bcp de classes déjà. Je ne voulais pas créer des classes supplémentaires avec uniquement une méthode (LogToHtml par exemple), pour que ce soit plus simple à utiliser au final, par mes collègues.
Par contre, une classe LogToXml, LogToFile...là je suis moins d'accord. Ca fragment bien trop, pour pas grand chose, à mon sens. La seule "sortie" que j'aurais effectivement dû faire, c'est LogToHtml. J'ai hésité, à vrai dire. Et on n'est pas obligé d'utiliser cette dernière méthode. J'ai bien fialli la virer, et mettre en forme uniquement dans log.detail.php (bref, dans une page php classique).

CouCou => par simple soucis de clarté. Quand on lit, on sait directement à quel libellé fait référence tel index numérique.

Commentaire de mfaraday le 28/04/2006 13:29:09

Salut

Je rebondit sur le commentaire de FhX et fait par la même une petite digression.

Je suis en train de développer des classes pour un projet perso et me posais justement la question concernant les fonctions retournant du (x)html... peut-on le faire dans une classe ou doit-on créer une fonction externe ?

Désolé pour ce petit écart...

Florian

Commentaire de mfaraday le 28/04/2006 21:09:15

Re

En fait ma question précédente n'est pas une question de possibilité mais d'opportunité...

Est-il plus opportun (utile ? logique ?) de mettre un méthode dans une classe retournant le html ou juste une fonction hors de la classe.

Florian

Commentaire de malalam le 26/08/2006 20:49:33 administrateur CS

Hello,

désolé, je n'avais jamais vu ce message...je réponds avec un peu de retard lol... ;-)
En bonne POO, il ne faut pas mélanger le html et la classe. Donc non. D'un autre côté, PHP génère du html...alors...c'est un peu chacun qui voit midi à sa porte, au fond.
PHP5 offre de base une méthode magique retournant l'objet sous forme de chaîne...on peut en déduire que les dév de php ne sont pas contre ce mélange (__toString).

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

Classe XML pour php4 [ par dandy54 ] Salut !Exist-t-il une classe compatible avec php4 pour manipuler les ficheirs Xml et si possible aussi simple que SimpleXml (oui je suis exigeant) ??M Visibilité des membres d'une classe avec autoload ? [ par petitelarve ] Bonjour, ca m'&#233;n&#233;rve !!! J'ai une classe que je veut instancier dans un autre script avec autoload. L'objectif &#233;tant de r&#233;cup&#233 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 Lecture de fichier XML [ par stigmate101 ] Bonjour,J'ai écris un code qui dois chercher un enregistrement dans un fichier XML. Ce code ne me m'affiche rien et ne me retourne pas d'erreur! Cl Appel d'une classe dans une classe [ par qntoinOo ] Bonjour à tous,Je suis face à un petit problème, je fait appel dans mon site à plusieurs classes une pour mes requêtes mysql, une pour mon système de classe générique pour parser tout type de fichier xml [ par mams004 ] Bonjour, je cherche desespérement une classe php générique pour parser tout type de ficher XML Si vous connaisser des liens ou tuto? merci pour Faut il un parseur XML ou tout réécrire ? [ par slhuilli ] Bonjour, Je bosse sur un flux XML que je trouve très bizarre (mais c'est peut etre moi qui me trompe !). Ce flux est généré par google meteo. Je vous probleme d'instance de classe [ par gretata ] bonjour a tous !! j'ai un problème au niveau des objets en PHP, je ne comprend pas bien comment ils fonctionnent. j'essai de faire un get sur un obj Erreur xml [ par ouzb ] Bonjour je veux recuperer des infos envoyez par un client en xml. Le client utilise curl pour les envoyer et je dois le recuperer en php par une métho J'aurai besoin d'un peut d'aide svp [ par Underskill ] Bonsoir J'aurais besoin d'aide. Je n'arrive pas afficher les fonction correctement pour afficher le script esceque vous pouriez m'aider svp Le script


Nos sponsors


Sondage...

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 1,232 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales