test new loger and add some phpdoc

This commit is contained in:
Manuel Raynaud
2013-01-21 10:55:48 +01:00
parent ad17c5d99e
commit 02139a938d
3 changed files with 219 additions and 49 deletions

View File

@@ -44,7 +44,7 @@ class TlogDestinationText extends AbstractTlogDestination
public function add($texte)
{
echo $texte."\n";
echo trim($texte)."\n";
}
public function write(&$res)

View File

@@ -80,6 +80,10 @@ class Tlog Implements LoggerInterface
*/
private static $instance = false;
/**
*
* @var array containing class of destination handler
*/
protected $destinations = array();
protected $mode_back_office = false;
@@ -118,6 +122,9 @@ class Tlog Implements LoggerInterface
return self::$instance;
}
/**
* initialize default configuration
*/
protected function init()
{
@@ -141,6 +148,10 @@ class Tlog Implements LoggerInterface
// Configuration
// -------------
/**
*
* @param string $destinations
*/
public function setDestinations($destinations)
{
if (! empty($destinations)) {
@@ -151,7 +162,13 @@ class Tlog Implements LoggerInterface
$this->loadDestinations($this->destinations, $classes_destinations);
}
}
/**
*
* change the debug level. Use Tlog constant : \Thelia\Log\Tlog::DEBUG set level to Debug
*
* @param int $level
*/
public function setLevel($level)
{
$this->level = $level;
@@ -201,48 +218,141 @@ class Tlog Implements LoggerInterface
// Methodes d'accès aux traces
// ---------------------------
/**
* Detailed debug information.
*
* @param string $message
* @param array $context
* @return null
*/
public function debug($message, array $context = array())
{
$this->log(self::DEBUG, $message, $context);
}
/**
* Interesting events.
*
* Example: User logs in, SQL logs.
*
* @param string $message
* @param array $context
* @return null
*/
public function info($message, array $context = array())
{
$this->log(self::INFO, $message, $context);
}
/**
* Normal but significant events.
*
* @param string $message
* @param array $context
* @return null
*/
public function notice($message, array $context = array())
{
$this->log(self::NOTICE, $message, $context);
}
/**
* Exceptional occurrences that are not errors.
*
* Example: Use of deprecated APIs, poor use of an API, undesirable things
* that are not necessarily wrong.
*
* @param string $message
* @param array $context
* @return null
*/
public function warning($message, array $context = array())
{
$this->log(self::WARNING, $message, $context);
}
/**
* Runtime errors that do not require immediate action but should typically
* be logged and monitored.
*
* @param string $message
* @param array $context
* @return null
*/
public function error($message, array $context = array())
{
$this->log(self::ERROR, $message, $context);
}
/**
*
* @see error()
*/
public function err($message, array $context = array())
{
$this->error($message, $context);
}
/**
* Critical conditions.
*
* Example: Application component unavailable, unexpected exception.
*
* @param string $message
* @param array $context
* @return null
*/
public function critical($message, array $context = array())
{
$this->log(self::CRITICAL, $message, $context);
}
/**
*
* @see critical()
*/
public function crit($message, array $context = array())
{
$this->critical($message, $context);
}
/**
* Action must be taken immediately.
*
* Example: Entire website down, database unavailable, etc. This should
* trigger the SMS alerts and wake you up.
*
* @param string $message
* @param array $context
* @return null
*/
public function alert($message, array $context = array())
{
$this->log(self::ALERT, $message, $context);
}
/**
* System is unusable.
*
* @param string $message
* @param array $context
* @return null
*/
public function emergency($message, array $context = array())
{
$this->log(self::EMERGENCY, $message, $context);
}
/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
* @return null
*/
public function log($level, $message, array $context = array()) {
if($this->level > $level && array_key_exists($level, $this->levels) === false)
if($this->level > $level || array_key_exists($level, $this->levels) === false)
return;
$this->out($this->levels[$level], $message, $context);
@@ -253,12 +363,18 @@ class Tlog Implements LoggerInterface
// Mode back office
public static function SetBackOfficeMode($booleen)
{
foreach (Tlog::instance()->destinations as $dest) {
foreach (Tlog::getInstance()->destinations as $dest) {
$dest->SetBackOfficeMode($booleen);
}
}
// Ecriture finale
/**
*
* final end method. Write log for each destination handler
*
* @param string $res
* @return void
*/
public function write(&$res)
{
self::$done = true;
@@ -271,7 +387,9 @@ class Tlog Implements LoggerInterface
}
}
//function register into register shutdown function stack
/**
* @see write()
*/
public function writeOnExit()
{
// Si les infos de debug n'ont pas été ecrites, le faire maintenant
@@ -303,8 +421,13 @@ class Tlog Implements LoggerInterface
}
}
// Permet de déterminer si la trace est active, en prenant en compte
// le level et le filtrage par fichier
/**
*
* check if level is activated and control if current file is activated
*
* @param int $level
* @return boolean
*/
public function isActivated($level)
{
if ($this->level <= $level) {
@@ -320,7 +443,14 @@ class Tlog Implements LoggerInterface
return false;
}
/**
*
* check if $file is in authorized files
*
* @param string $file
* @return boolean
*/
public function isActivedFile($file)
{
return ($this->all_files || in_array($file, $this->files)) && ! in_array("!$file", $this->files);
@@ -328,7 +458,7 @@ class Tlog Implements LoggerInterface
/* -- Methodes privees ---------------------------------------- */
// Adapté de LoggerLoginEvent dans log4php
private function findOrigin()
{
$origine = array();

View File

@@ -30,37 +30,17 @@ class TlogTest extends \PHPUnit_Framework_TestCase
{
protected static $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";
protected $regex = "/(\\d)(: %s)(\\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)([\n])/is";
public static function setUpBeforeClass()
{
self::$logger = Tlog::getInstance();
self::$logger->setDestinations("Thelia\Log\Destination\TlogDestinationText");
self::$logger->setLevel(Tlog::TRACE);
self::$logger->setLevel(Tlog::DEBUG);
self::$logger->setFiles("*");
}
public function testTraceWithTraceLevel()
{
$logger = self::$logger;
$logger->setLevel(Tlog::TRACE);
//"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: "
$this->expectOutputRegex(sprintf($this->regex, "foo"));
$logger->trace("foo");
}
public function testTraceWitoutTraceLevel()
{
$logger = self::$logger;
$logger->setLevel(Tlog::MUET);
$this->expectOutputRegex("/^$/");
$logger->trace("foo");
}
public function testDebugWithDebugLevel()
{
@@ -68,11 +48,11 @@ class TlogTest extends \PHPUnit_Framework_TestCase
$logger->setLevel(Tlog::DEBUG);
//"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: "
$this->expectOutputRegex(sprintf($this->regex, "foo"));
$this->expectOutputRegex(sprintf($this->regex, "DEBUG", "foo"));
$logger->debug("foo");
}
public function testDebugWitoutDebugLevel()
public function testDebugWithoutDebugLevel()
{
$logger = self::$logger;
$logger->setLevel(Tlog::MUET);
@@ -80,19 +60,19 @@ class TlogTest extends \PHPUnit_Framework_TestCase
$this->expectOutputRegex("/^$/");
$logger->debug("foo");
}
public function testInfoWithInfoLevel()
public function testDebugWithInfoLevel()
{
$logger = self::$logger;
$logger->setLevel(Tlog::INFO);
//"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: "
$this->expectOutputRegex(sprintf($this->regex, "foo"));
$this->expectOutputRegex(sprintf($this->regex, "INFO", "foo"));
$logger->info("foo");
}
public function testInfoWitoutInfoLevel()
public function testDebugWithoutInfoLevel()
{
$logger = self::$logger;
$logger->setLevel(Tlog::MUET);
@@ -100,6 +80,26 @@ class TlogTest extends \PHPUnit_Framework_TestCase
$this->expectOutputRegex("/^$/");
$logger->info("foo");
}
public function testDebugWithNoticeLevel()
{
$logger = self::$logger;
$logger->setLevel(Tlog::NOTICE);
//"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: "
$this->expectOutputRegex(sprintf($this->regex, "NOTICE", "foo"));
$logger->notice("foo");
}
public function testDebugWithoutNoticeLevel()
{
$logger = self::$logger;
$logger->setLevel(Tlog::MUET);
$this->expectOutputRegex("/^$/");
$logger->notice("foo");
}
public function testWarningWithWarningLevel()
{
@@ -108,11 +108,11 @@ class TlogTest extends \PHPUnit_Framework_TestCase
$logger->setLevel(Tlog::WARNING);
//"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: "
$this->expectOutputRegex(sprintf($this->regex, "foo"));
$this->expectOutputRegex(sprintf($this->regex, "WARNING", "foo"));
$logger->warning("foo");
}
public function testWarningWitoutWarningLevel()
public function testWarningWithoutWarningLevel()
{
$logger = self::$logger;
$logger->setLevel(Tlog::MUET);
@@ -128,11 +128,11 @@ class TlogTest extends \PHPUnit_Framework_TestCase
$logger->setLevel(Tlog::ERROR);
//"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: "
$this->expectOutputRegex(sprintf($this->regex, "foo"));
$this->expectOutputRegex(sprintf($this->regex, "ERROR", "foo"));
$logger->error("foo");
}
public function testErrorWitoutErrorLevel()
public function testErrorWithoutErrorLevel()
{
$logger = self::$logger;
$logger->setLevel(Tlog::MUET);
@@ -140,24 +140,64 @@ class TlogTest extends \PHPUnit_Framework_TestCase
$this->expectOutputRegex("/^$/");
$logger->error("foo");
}
public function testFatalWithFatalLevel()
public function testErrorWithCriticalLevel()
{
$logger = self::$logger;
$logger->setLevel(Tlog::FATAL);
$logger->setLevel(Tlog::CRITICAL);
//"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: "
$this->expectOutputRegex(sprintf($this->regex, "foo"));
$logger->fatal("foo");
$this->expectOutputRegex(sprintf($this->regex, "CRITICAL", "foo"));
$logger->critical("foo");
}
public function testFatalWitoutFatalLevel()
public function testErrorWithoutCriticalLevel()
{
$logger = self::$logger;
$logger->setLevel(Tlog::MUET);
$this->expectOutputRegex("/^$/");
$logger->fatal("foo");
$logger->critical("foo");
}
public function testErrorWithAlertLevel()
{
$logger = self::$logger;
$logger->setLevel(Tlog::ALERT);
//"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: "
$this->expectOutputRegex(sprintf($this->regex, "ALERT", "foo"));
$logger->alert("foo");
}
public function testErrorWithoutAlertLevel()
{
$logger = self::$logger;
$logger->setLevel(Tlog::MUET);
$this->expectOutputRegex("/^$/");
$logger->alert("foo");
}
public function testErrorWithEmergencyLevel()
{
$logger = self::$logger;
$logger->setLevel(Tlog::EMERGENCY);
//"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: "
$this->expectOutputRegex(sprintf($this->regex, "EMERGENCY", "foo"));
$logger->emergency("foo");
}
public function testErrorWithoutEmergencyLevel()
{
$logger = self::$logger;
$logger->setLevel(Tlog::MUET);
$this->expectOutputRegex("/^$/");
$logger->emergency("foo");
}
}