diff --git a/core/lib/Thelia/Log/Destination/TlogDestinationNull.php b/core/lib/Thelia/Log/Destination/TlogDestinationNull.php new file mode 100644 index 000000000..caa6108d1 --- /dev/null +++ b/core/lib/Thelia/Log/Destination/TlogDestinationNull.php @@ -0,0 +1,45 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Log\Destination; + +use Thelia\Log\AbstractTlogDestination; + +class TlogDestinationNull extends AbstractTlogDestination { + + public function get_titre() { + return "Trou noir"; + } + + public function get_description() { + return "Cette destination ne provoque aucune sortie"; + } + + public function ajouter($string) { + // Rien + } + + public function ecrire(&$res) { + // Rien + } +} diff --git a/core/lib/Thelia/Log/Destination/TlogDestinationText.php b/core/lib/Thelia/Log/Destination/TlogDestinationText.php new file mode 100644 index 000000000..b390b9b7b --- /dev/null +++ b/core/lib/Thelia/Log/Destination/TlogDestinationText.php @@ -0,0 +1,50 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Log\Destination; + +use Thelia\Log\AbstractTlogDestination; + + +class TlogDestinationText extends AbstractTlogDestination { + + public function __construct() { + parent::__construct(); + } + + public function get_titre() { + return "Affichage direct dans la page, en texte brut"; + } + + public function get_description() { + return "Permet d'afficher les logs directement dans la page resultat, au format texte brut."; + } + + public function ajouter($texte) { + echo $texte."\n"; + } + + public function ecrire(&$res) { + // Rien + } +} diff --git a/core/lib/Thelia/Log/Tlog.php b/core/lib/Thelia/Log/Tlog.php index f42083f29..1ae8316aa 100644 --- a/core/lib/Thelia/Log/Tlog.php +++ b/core/lib/Thelia/Log/Tlog.php @@ -54,7 +54,7 @@ class Tlog Implements TlogInterface // default values const DEFAULT_LEVEL = self::DEBUG; - const DEFAUT_DESTINATIONS = "Thelia\Log\Destination\TlogDestinationFile"; + const DEFAUT_DESTINATIONS = "Thelia\Log\Destination\TlogDestinationNull"; const DEFAUT_PREFIXE = "#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: "; const DEFAUT_FILES = "*"; const DEFAUT_IP = ""; @@ -97,7 +97,7 @@ class Tlog Implements TlogInterface // if (self::$instance == false) { // self::$instance = new Tlog(); // -// // On doit placer les initialisations à ce niveau pour pouvoir +// // On doit placer les initialisations à ce level 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(); @@ -111,7 +111,7 @@ class Tlog Implements TlogInterface $level = $config->read(self::VAR_LEVEL, self::DEFAULT_LEVEL); - $this->set_niveau($level); + $this->set_level($level); $this->dir_destinations = array( __DIR__.'/Destination' @@ -141,8 +141,8 @@ class Tlog Implements TlogInterface } } - public function set_niveau($level) { - $this->niveau = $level; + public function set_level($level) { + $this->level = $level; } public function set_prefixe($prefixe) { @@ -157,7 +157,7 @@ class Tlog Implements TlogInterface } public function set_ip($ips) { - if (! empty($ips) && ! in_array($_SERVER['REMOTE_ADDR'], explode(";", $ips))) $this->niveau = self::MUET; + if (! empty($ips) && ! in_array($_SERVER['REMOTE_ADDR'], explode(";", $ips))) $this->level = self::MUET; } public function set_show_redirect($bool) { @@ -186,7 +186,7 @@ class Tlog Implements TlogInterface // --------------------------- public function trace() { - if ($this->niveau > self::TRACE) + if ($this->level > self::TRACE) return; $args = func_get_args(); @@ -195,7 +195,7 @@ class Tlog Implements TlogInterface } public function debug() { - if ($this->niveau > self::DEBUG) + if ($this->level > self::DEBUG) return; $args = func_get_args(); @@ -204,7 +204,7 @@ class Tlog Implements TlogInterface } public function info() { - if ($this->niveau > self::INFO) + if ($this->level > self::INFO) return; $args = func_get_args(); @@ -213,7 +213,7 @@ class Tlog Implements TlogInterface } public function warning() { - if (Tlog::instance()->niveau > self::WARNING) + if ($this->level > self::WARNING) return; $args = func_get_args(); @@ -222,7 +222,7 @@ class Tlog Implements TlogInterface } public function error() { - if ($this->niveau > self::ERROR) + if ($this->level > self::ERROR) return; $args = func_get_args(); @@ -231,7 +231,7 @@ class Tlog Implements TlogInterface } public function fatal() { - if ($this->niveau > self::FATAL) + if ($this->level > self::FATAL) return; $args = func_get_args(); @@ -253,7 +253,7 @@ class Tlog Implements TlogInterface self::$ecrire_effectue = true; // Muet ? On ne fait rien - if ($this->niveau == self::MUET) return; + if ($this->level == self::MUET) return; foreach($this->destinations as $dest) { $dest->ecrire($res); @@ -274,7 +274,7 @@ class Tlog Implements TlogInterface public function afficher_redirections($url) { - if ($this->niveau != self::MUET && $this->show_redirect) { + if ($this->level != self::MUET && $this->show_redirect) { echo " Redirection... @@ -292,10 +292,10 @@ class Tlog Implements TlogInterface } // Permet de déterminer si la trace est active, en prenant en compte - // le niveau et le filtrage par fichier + // le level et le filtrage par fichier public function active($level) { - if ($this->niveau <= $level) { + if ($this->level <= $level) { $origine = $this->trouver_origine(); diff --git a/core/lib/Thelia/Tests/Log/TlogTest.php b/core/lib/Thelia/Tests/Log/TlogTest.php new file mode 100644 index 000000000..451b594eb --- /dev/null +++ b/core/lib/Thelia/Tests/Log/TlogTest.php @@ -0,0 +1,187 @@ +. */ +/* */ +/*************************************************************************************/ + + +namespace Thelia\Tests\Log; + +use Thelia\Log\Tlog; +use Thelia\Core\Thelia; + +class TlogTest extends \PHPUnit_Framework_TestCase +{ + protected $logger; + + protected $regex = "/(\\d)(.)(\\s+)((?:[a-z][a-z]+))(\\s+)(\\[.*?\\])(\\s+)(\\{.*?\\})(\\s+)((?:2|1)\\d{3}(?:-|\\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]))(.)(\\s+)(%s)/is"; + + public function setUp() { + parent::setUp(); + + $_SERVER['REMOTE_ADDR'] = '::1'; + + $containerMock = $this->getMock("Symfony\Component\DependencyInjection\Container", array("get", "getParameter")); + + $configModel = new ConfigModel(); + + $containerMock->expects($this->any()) + ->method("get") + ->will($this->returnValue($configModel)); + $containerMock->expects($this->any()) + ->method("getParameter") + ->will($this->returnValue("Thelia\Log\Tlog")); + + + $this->logger = new Tlog($containerMock); + + $this->logger->set_destinations("Thelia\Log\Destination\TlogDestinationText"); + $this->logger->set_level(Tlog::TRACE); + $this->logger->set_files("*"); + } + + public function testTraceWithTraceLevel() + { + + $logger = $this->logger; + $logger->set_level(Tlog::TRACE); + + //"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: " + $this->expectOutputRegex(sprintf($this->regex, "foo")); + $logger->trace("foo"); + } + + public function testTraceWitoutTraceLevel() + { + $logger = $this->logger; + $logger->set_level(Tlog::MUET); + + $this->expectOutputRegex("/^$/"); + $logger->trace("foo"); + } + + public function testDebugWithDebugLevel() + { + + $logger = $this->logger; + $logger->set_level(Tlog::DEBUG); + + //"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: " + $this->expectOutputRegex(sprintf($this->regex, "foo")); + $logger->debug("foo"); + } + + public function testDebugWitoutDebugLevel() + { + $logger = $this->logger; + $logger->set_level(Tlog::MUET); + + $this->expectOutputRegex("/^$/"); + $logger->debug("foo"); + } + + public function testInfoWithInfoLevel() + { + + $logger = $this->logger; + $logger->set_level(Tlog::INFO); + + //"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: " + $this->expectOutputRegex(sprintf($this->regex, "foo")); + $logger->info("foo"); + } + + public function testInfoWitoutInfoLevel() + { + $logger = $this->logger; + $logger->set_level(Tlog::MUET); + + $this->expectOutputRegex("/^$/"); + $logger->info("foo"); + } + + public function testWarningWithWarningLevel() + { + + $logger = $this->logger; + $logger->set_level(Tlog::WARNING); + + //"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: " + $this->expectOutputRegex(sprintf($this->regex, "foo")); + $logger->warning("foo"); + } + + public function testWarningWitoutWarningLevel() + { + $logger = $this->logger; + $logger->set_level(Tlog::MUET); + + $this->expectOutputRegex("/^$/"); + $logger->warning("foo"); + } + + public function testErrorWithErrorLevel() + { + + $logger = $this->logger; + $logger->set_level(Tlog::ERROR); + + //"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: " + $this->expectOutputRegex(sprintf($this->regex, "foo")); + $logger->error("foo"); + } + + public function testErrorWitoutErrorLevel() + { + $logger = $this->logger; + $logger->set_level(Tlog::MUET); + + $this->expectOutputRegex("/^$/"); + $logger->error("foo"); + } + + public function testFatalWithFatalLevel() + { + + $logger = $this->logger; + $logger->set_level(Tlog::FATAL); + + //"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: " + $this->expectOutputRegex(sprintf($this->regex, "foo")); + $logger->fatal("foo"); + } + + public function testFatalWitoutFatalLevel() + { + $logger = $this->logger; + $logger->set_level(Tlog::MUET); + + $this->expectOutputRegex("/^$/"); + $logger->fatal("foo"); + } +} + + +class ConfigModel +{ + public function __call($name, $arguments) { + return $arguments[1]; + } +} \ No newline at end of file