configure Tlog and TlogFile
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
namespace Thelia\Log;
|
||||
|
||||
use Thelia\Model\Config;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -32,7 +33,7 @@ use Thelia\Model\Config;
|
||||
*
|
||||
* @author Franck Allimant <franck@cqfdev.fr>
|
||||
*/
|
||||
class Tlog
|
||||
class Tlog Implements TlogInterface
|
||||
{
|
||||
// Nom des variables de configuration
|
||||
const VAR_LEVEL = "tlog_level";
|
||||
@@ -52,8 +53,8 @@ class Tlog
|
||||
const MUET = PHP_INT_MAX;
|
||||
|
||||
// default values
|
||||
const DEFAULT_LEVEL = self::MUET;
|
||||
const DEFAUT_DESTINATIONS = "Thelia\Log\Destination\TlogDestinationHtml";
|
||||
const DEFAULT_LEVEL = self::DEBUG;
|
||||
const DEFAUT_DESTINATIONS = "Thelia\Log\Destination\TlogDestinationFile";
|
||||
const DEFAUT_PREFIXE = "#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: ";
|
||||
const DEFAUT_FILES = "*";
|
||||
const DEFAUT_IP = "";
|
||||
@@ -76,22 +77,39 @@ class Tlog
|
||||
|
||||
// directories where are the Destinations Files
|
||||
public $dir_destinations = array();
|
||||
|
||||
public static function instance() {
|
||||
if (self::$instance == false) {
|
||||
self::$instance = new Tlog();
|
||||
|
||||
// On doit placer les initialisations à ce niveau pour pouvoir
|
||||
// utiliser la classe Tlog dans les classes de base (Cnx, BaseObj, etc.)
|
||||
// Les placer dans le constructeur provoquerait une boucle
|
||||
self::$instance->init();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param type $container
|
||||
*
|
||||
* public function __construct(ContainerBuilder $container)
|
||||
*/
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
|
||||
$this->init();
|
||||
}
|
||||
|
||||
// public static function instance() {
|
||||
// if (self::$instance == false) {
|
||||
// self::$instance = new Tlog();
|
||||
//
|
||||
// // On doit placer les initialisations à ce niveau pour pouvoir
|
||||
// // utiliser la classe Tlog dans les classes de base (Cnx, BaseObj, etc.)
|
||||
// // Les placer dans le constructeur provoquerait une boucle
|
||||
// self::$instance->init();
|
||||
// }
|
||||
//
|
||||
// return self::$instance;
|
||||
// }
|
||||
|
||||
protected function init() {
|
||||
$level = Config::read(self::VAR_LEVEL, self::DEFAULT_LEVEL);
|
||||
$config = $this->container->get("model.config");
|
||||
|
||||
$level = $config->read(self::VAR_LEVEL, self::DEFAULT_LEVEL);
|
||||
|
||||
$this->set_niveau($level);
|
||||
|
||||
@@ -100,11 +118,11 @@ class Tlog
|
||||
//, __DIR__.'/../client/tlog/destinations'
|
||||
);
|
||||
|
||||
$this->set_prefixe(Config::read(self::VAR_PREFIXE, self::DEFAUT_PREFIXE));
|
||||
$this->set_files(Config::read(self::VAR_FILES, self::DEFAUT_FILES));
|
||||
$this->set_ip(Config::read(self::VAR_IP, self::DEFAUT_IP));
|
||||
$this->set_destinations(Config::read(self::VAR_DESTINATIONS, self::DEFAUT_DESTINATIONS));
|
||||
$this->set_show_redirect(Config::read(self::VAR_SHOW_REDIRECT, self::DEFAUT_SHOW_REDIRECT));
|
||||
$this->set_prefixe($config->read(self::VAR_PREFIXE, self::DEFAUT_PREFIXE));
|
||||
$this->set_files($config->read(self::VAR_FILES, self::DEFAUT_FILES));
|
||||
$this->set_ip($config->read(self::VAR_IP, self::DEFAUT_IP));
|
||||
$this->set_destinations($config->read(self::VAR_DESTINATIONS, self::DEFAUT_DESTINATIONS));
|
||||
$this->set_show_redirect($config->read(self::VAR_SHOW_REDIRECT, self::DEFAUT_SHOW_REDIRECT));
|
||||
|
||||
// Au cas ou il y aurait un exit() quelque part dans le code.
|
||||
register_shutdown_function(array($this, 'ecrire_si_exit'));
|
||||
@@ -113,13 +131,13 @@ class Tlog
|
||||
// Configuration
|
||||
// -------------
|
||||
|
||||
public function set_destinations(AbstractTlogDestination $destinations) {
|
||||
public function set_destinations($destinations) {
|
||||
if (! empty($destinations)) {
|
||||
|
||||
$this->destinations = array();
|
||||
|
||||
$classes_destinations = explode(';', $destinations);
|
||||
$this->charger_classes_destinations($this->destinations, $classes_destinations);
|
||||
$this->loadDestinations($this->destinations, $classes_destinations);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,179 +185,179 @@ class Tlog
|
||||
// Methodes d'accès aux traces
|
||||
// ---------------------------
|
||||
|
||||
public static function trace() {
|
||||
if (Tlog::instance()->niveau > self::TRACE)
|
||||
public function trace() {
|
||||
if ($this->niveau > self::TRACE)
|
||||
return;
|
||||
|
||||
$args = func_get_args();
|
||||
|
||||
Tlog::instance()->out("TRACE", $args);
|
||||
$this->out("TRACE", $args);
|
||||
}
|
||||
|
||||
public static function debug() {
|
||||
if (Tlog::instance()->niveau > self::DEBUG)
|
||||
public function debug() {
|
||||
if ($this->niveau > self::DEBUG)
|
||||
return;
|
||||
|
||||
$args = func_get_args();
|
||||
|
||||
Tlog::instance()->out("DEBUG", $args);
|
||||
$this->out("DEBUG", $args);
|
||||
}
|
||||
|
||||
public static function info() {
|
||||
if (Tlog::instance()->niveau > self::INFO)
|
||||
public function info() {
|
||||
if ($this->niveau > self::INFO)
|
||||
return;
|
||||
|
||||
$args = func_get_args();
|
||||
|
||||
Tlog::instance()->out("INFO", $args);
|
||||
$this->out("INFO", $args);
|
||||
}
|
||||
|
||||
public static function warning() {
|
||||
public function warning() {
|
||||
if (Tlog::instance()->niveau > self::WARNING)
|
||||
return;
|
||||
|
||||
$args = func_get_args();
|
||||
|
||||
Tlog::instance()->out("WARNING", $args);
|
||||
$this->out("WARNING", $args);
|
||||
}
|
||||
|
||||
public static function error() {
|
||||
if (Tlog::instance()->niveau > self::ERROR)
|
||||
public function error() {
|
||||
if ($this->niveau > self::ERROR)
|
||||
return;
|
||||
|
||||
$args = func_get_args();
|
||||
|
||||
Tlog::instance()->out("ERREUR", $args);
|
||||
$this->out("ERREUR", $args);
|
||||
}
|
||||
|
||||
public static function fatal() {
|
||||
if (Tlog::instance()->niveau > self::FATAL)
|
||||
public function fatal() {
|
||||
if ($this->niveau > self::FATAL)
|
||||
return;
|
||||
|
||||
$args = func_get_args();
|
||||
|
||||
Tlog::instance()->out("FATAL", $args);
|
||||
$this->out("FATAL", $args);
|
||||
}
|
||||
|
||||
// Mode back office
|
||||
public static function mode_back_office($booleen) {
|
||||
// Mode back office
|
||||
public static function mode_back_office($booleen) {
|
||||
|
||||
foreach(Tlog::instance()->destinations as $dest) {
|
||||
$dest->mode_back_office($booleen);
|
||||
}
|
||||
}
|
||||
foreach(Tlog::instance()->destinations as $dest) {
|
||||
$dest->mode_back_office($booleen);
|
||||
}
|
||||
}
|
||||
|
||||
// Ecriture finale
|
||||
public static function ecrire(&$res) {
|
||||
// Ecriture finale
|
||||
public function ecrire(&$res) {
|
||||
|
||||
self::$ecrire_effectue = true;
|
||||
self::$ecrire_effectue = true;
|
||||
|
||||
// Muet ? On ne fait rien
|
||||
if (Tlog::instance()->niveau == self::MUET) return;
|
||||
// Muet ? On ne fait rien
|
||||
if ($this->niveau == self::MUET) return;
|
||||
|
||||
foreach(Tlog::instance()->destinations as $dest) {
|
||||
$dest->ecrire($res);
|
||||
}
|
||||
}
|
||||
foreach($this->destinations as $dest) {
|
||||
$dest->ecrire($res);
|
||||
}
|
||||
}
|
||||
|
||||
public static function ecrire_si_exit() {
|
||||
// Si les infos de debug n'ont pas été ecrites, le faire maintenant
|
||||
if (self::$ecrire_effectue === false) {
|
||||
public function ecrire_si_exit() {
|
||||
// Si les infos de debug n'ont pas été ecrites, le faire maintenant
|
||||
if (self::$ecrire_effectue === false) {
|
||||
|
||||
$res = "";
|
||||
$res = "";
|
||||
|
||||
self::ecrire($res);
|
||||
$this->ecrire($res);
|
||||
|
||||
echo $res;
|
||||
}
|
||||
}
|
||||
echo $res;
|
||||
}
|
||||
}
|
||||
|
||||
public function afficher_redirections($url) {
|
||||
public function afficher_redirections($url) {
|
||||
|
||||
if ($this->niveau != self::MUET && $this->show_redirect) {
|
||||
echo "
|
||||
if ($this->niveau != self::MUET && $this->show_redirect) {
|
||||
echo "
|
||||
<html>
|
||||
<head><title>Redirection...</title></head>
|
||||
<body>
|
||||
<a href=\"$url\">Redirection vers $url</a>
|
||||
<a href=\"$url\">Redirection vers $url</a>
|
||||
</body>
|
||||
</html>
|
||||
";
|
||||
";
|
||||
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Permet de déterminer si la trace est active, en prenant en compte
|
||||
// le niveau et le filtrage par fichier
|
||||
public function active($level) {
|
||||
// Permet de déterminer si la trace est active, en prenant en compte
|
||||
// le niveau et le filtrage par fichier
|
||||
public function active($level) {
|
||||
|
||||
if ($this->niveau <= $level) {
|
||||
if ($this->niveau <= $level) {
|
||||
|
||||
$origine = $this->trouver_origine();
|
||||
$origine = $this->trouver_origine();
|
||||
|
||||
$file = basename($origine['file']);
|
||||
$file = basename($origine['file']);
|
||||
|
||||
if ($this->is_file_active($file)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if ($this->is_file_active($file)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function is_file_active($file) {
|
||||
return ($this->all_files || in_array($file, $this->files)) && ! in_array("!$file", $this->files);
|
||||
}
|
||||
public function is_file_active($file) {
|
||||
return ($this->all_files || in_array($file, $this->files)) && ! in_array("!$file", $this->files);
|
||||
}
|
||||
|
||||
/* -- Methodes privees ---------------------------------------- */
|
||||
/* -- Methodes privees ---------------------------------------- */
|
||||
|
||||
// Adapté de LoggerLoginEvent dans log4php
|
||||
private function trouver_origine() {
|
||||
// Adapté de LoggerLoginEvent dans log4php
|
||||
private function trouver_origine() {
|
||||
|
||||
$origine = array();
|
||||
$origine = array();
|
||||
|
||||
if(function_exists('debug_backtrace')) {
|
||||
if(function_exists('debug_backtrace')) {
|
||||
|
||||
$trace = debug_backtrace();
|
||||
$prevHop = null;
|
||||
// make a downsearch to identify the caller
|
||||
$hop = array_pop($trace);
|
||||
$trace = debug_backtrace();
|
||||
$prevHop = null;
|
||||
// make a downsearch to identify the caller
|
||||
$hop = array_pop($trace);
|
||||
|
||||
while($hop !== null) {
|
||||
if(isset($hop['class'])) {
|
||||
// we are sometimes in functions = no class available: avoid php warning here
|
||||
$className = $hop['class'];
|
||||
|
||||
while($hop !== null) {
|
||||
if(isset($hop['class'])) {
|
||||
// we are sometimes in functions = no class available: avoid php warning here
|
||||
$className = strtolower($hop['class']);
|
||||
if (! empty($className) and ($className == ltrim($this->container->getParameter("logger.class"),'\\') or strtolower(get_parent_class($className)) == ltrim($this->container->getParameter("logger.class"),'\\'))) {
|
||||
$origine['line'] = $hop['line'];
|
||||
$origine['file'] = $hop['file'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
$prevHop = $hop;
|
||||
$hop = array_pop($trace);
|
||||
}
|
||||
|
||||
if (! empty($className) and ($className == 'tlog' or strtolower(get_parent_class($className)) == 'tlog')) {
|
||||
$origine['line'] = $hop['line'];
|
||||
$origine['file'] = $hop['file'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
$prevHop = $hop;
|
||||
$hop = array_pop($trace);
|
||||
}
|
||||
$origine['class'] = isset($prevHop['class']) ? $prevHop['class'] : 'main';
|
||||
|
||||
$origine['class'] = isset($prevHop['class']) ? $prevHop['class'] : 'main';
|
||||
if(isset($prevHop['function']) and
|
||||
$prevHop['function'] !== 'include' and
|
||||
$prevHop['function'] !== 'include_once' and
|
||||
$prevHop['function'] !== 'require' and
|
||||
$prevHop['function'] !== 'require_once') {
|
||||
|
||||
if(isset($prevHop['function']) and
|
||||
$prevHop['function'] !== 'include' and
|
||||
$prevHop['function'] !== 'include_once' and
|
||||
$prevHop['function'] !== 'require' and
|
||||
$prevHop['function'] !== 'require_once') {
|
||||
$origine['function'] = $prevHop['function'];
|
||||
} else {
|
||||
$origine['function'] = 'main';
|
||||
}
|
||||
}
|
||||
|
||||
$origine['function'] = $prevHop['function'];
|
||||
} else {
|
||||
$origine['function'] = 'main';
|
||||
}
|
||||
}
|
||||
|
||||
return $origine;
|
||||
}
|
||||
return $origine;
|
||||
}
|
||||
|
||||
private function out($level, $tabargs) {
|
||||
|
||||
@@ -353,52 +371,46 @@ class Tlog
|
||||
|
||||
$file = basename($origine['file']);
|
||||
|
||||
if ($this->is_file_active($file)) {
|
||||
if ($this->is_file_active($file)) {
|
||||
|
||||
$function = $origine['function'];
|
||||
$line = $origine['line'];
|
||||
$function = $origine['function'];
|
||||
$line = $origine['line'];
|
||||
|
||||
$prefixe = str_replace(
|
||||
array("#NUM", "#NIVEAU", "#FICHIER", "#FONCTION", "#LIGNE", "#DATE", "#HEURE"),
|
||||
array(1+$this->linecount, $level, $file, $function, $line, date("Y-m-d"), date("G:i:s")),
|
||||
$this->prefixe
|
||||
);
|
||||
$prefixe = str_replace(
|
||||
array("#NUM", "#NIVEAU", "#FICHIER", "#FONCTION", "#LIGNE", "#DATE", "#HEURE"),
|
||||
array(1+$this->linecount, $level, $file, $function, $line, date("Y-m-d"), date("G:i:s")),
|
||||
$this->prefixe
|
||||
);
|
||||
|
||||
$trace = $prefixe . $text;
|
||||
$trace = $prefixe . $text;
|
||||
|
||||
foreach($this->destinations as $dest) {
|
||||
$dest->ajouter($trace);
|
||||
}
|
||||
foreach($this->destinations as $dest) {
|
||||
$dest->ajouter($trace);
|
||||
}
|
||||
|
||||
$this->linecount++;
|
||||
}
|
||||
$this->linecount++;
|
||||
}
|
||||
}
|
||||
|
||||
protected function charger_classes_destinations(&$destinations, $actives = false) {
|
||||
|
||||
foreach($this->dir_destinations as $dir) {
|
||||
$classes = array();
|
||||
|
||||
if ($dh = @opendir($dir)) {
|
||||
|
||||
while ($file = readdir($dh)) {
|
||||
|
||||
if ($file == '.' || $file == '..') continue;
|
||||
|
||||
$matches = array();
|
||||
|
||||
if (preg_match("/([^\.]+)\.class\.php/", $file, $matches)) {
|
||||
$classname = $matches[1];
|
||||
|
||||
if ( ($actives === false || in_array($classname, $actives)) && ! isset($destinations[$classname])) {
|
||||
include_once("$dir/$file");
|
||||
|
||||
$destinations[$classname] = new $classname();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param type $destinations
|
||||
* @param array $actives array containing classes instanceof AbstractTlogDestination
|
||||
*/
|
||||
protected function loadDestinations(&$destinations, array $actives = NULL) {
|
||||
|
||||
foreach($actives as $active)
|
||||
{
|
||||
if(class_exists($active))
|
||||
{
|
||||
$class = new $active($this->container->get('model.config'));
|
||||
|
||||
if(!$class instanceof AbstractTlogDestination)
|
||||
{
|
||||
throw new \UnexpectedValueException($active." must extends Thelia\Tlog\AbstractTlogDestination");
|
||||
}
|
||||
|
||||
@closedir($dh);
|
||||
|
||||
$destinations[$active] = $class;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user