You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ih...@apache.org on 2012/05/28 12:18:49 UTC

svn commit: r1343190 - in /logging/log4php/trunk: src/main/php/Logger.php src/main/php/LoggerRoot.php src/test/php/LoggerHierarchyTest.php src/test/php/LoggerRootTest.php tmp/

Author: ihabunek
Date: Mon May 28 10:18:49 2012
New Revision: 1343190

URL: http://svn.apache.org/viewvc?rev=1343190&view=rev
Log:
LOG4PHP-178: 
* Updated Logger::setLevel() to accept null.
* Updated LoggerRoot::setLevel() to deny null.
* Added error reporting to LoggerRoot.
* Updated tests.

Added:
    logging/log4php/trunk/tmp/
Modified:
    logging/log4php/trunk/src/main/php/Logger.php
    logging/log4php/trunk/src/main/php/LoggerRoot.php
    logging/log4php/trunk/src/test/php/LoggerHierarchyTest.php
    logging/log4php/trunk/src/test/php/LoggerRootTest.php

Modified: logging/log4php/trunk/src/main/php/Logger.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/Logger.php?rev=1343190&r1=1343189&r2=1343190&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/Logger.php (original)
+++ logging/log4php/trunk/src/main/php/Logger.php Mon May 28 10:18:49 2012
@@ -379,9 +379,9 @@ class Logger {
 	 * Use LoggerLevel::getLevelXXX() methods to get a LoggerLevel object, e.g.
 	 * <code>$logger->setLevel(LoggerLevel::getLevelInfo());</code>
 	 *
-	 * @param LoggerLevel $level the level to set
+	 * @param LoggerLevel $level The level to set, or NULL to clear the logger level.
 	 */
-	public function setLevel(LoggerLevel $level) {
+	public function setLevel(LoggerLevel $level = null) {
 		$this->level = $level;
 	}
 	
@@ -393,9 +393,8 @@ class Logger {
 	 */
 	public function isAttached(LoggerAppender $appender) {
 		return isset($this->appenders[$appender->getName()]);
-	} 
-		   
-
+	}
+	
 	/**
 	 * Sets the parent logger.
 	 * @param Logger $logger

Modified: logging/log4php/trunk/src/main/php/LoggerRoot.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/LoggerRoot.php?rev=1343190&r1=1343189&r2=1343190&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/LoggerRoot.php (original)
+++ logging/log4php/trunk/src/main/php/LoggerRoot.php Mon May 28 10:18:49 2012
@@ -31,7 +31,7 @@ class LoggerRoot extends Logger {
 	 *
 	 * @param integer $level initial log level
 	 */
-	public function __construct($level = null) {
+	public function __construct(LoggerLevel $level = null) {
 		parent::__construct('root');
 
 		if($level == null) {
@@ -43,28 +43,29 @@ class LoggerRoot extends Logger {
 	/**
 	 * @return LoggerLevel the level
 	 */
-	public function getChainedLevel() {
-		return parent::getLevel();
-	} 
+	public function getEffectiveLevel() {
+		return $this->getLevel();
+	}
 	
 	/**
-	 * Setting a null value to the level of the root category may have catastrophic results.
+	 * Override level setter to prevent setting the root logger's level to 
+	 * null. Root logger must always have a level.
+	 * 
 	 * @param LoggerLevel $level
 	 */
-	public function setLevel($level) {
-		if($level != null) {
+	public function setLevel(LoggerLevel $level = null) {
+		if (isset($level)) {
 			parent::setLevel($level);
-		}	 
+		} else {
+			trigger_error("log4php: Cannot set LoggerRoot level to null.", E_USER_WARNING);
+		}
 	}
 	
 	/**
-	 * Always returns false.
-	 * Because LoggerRoot has no parents, it returns false.
+	 * Override parent setter. Root logger cannot have a parent.
 	 * @param Logger $parent
-	 * @return boolean
 	 */
 	public function setParent(Logger $parent) {
-		return false;
+		trigger_error("log4php: LoggerRoot cannot have a parent.", E_USER_WARNING);
 	}
-
 }

Modified: logging/log4php/trunk/src/test/php/LoggerHierarchyTest.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/LoggerHierarchyTest.php?rev=1343190&r1=1343189&r2=1343190&view=diff
==============================================================================
--- logging/log4php/trunk/src/test/php/LoggerHierarchyTest.php (original)
+++ logging/log4php/trunk/src/test/php/LoggerHierarchyTest.php Mon May 28 10:18:49 2012
@@ -33,38 +33,23 @@ class LoggerHierarchyTest extends PHPUni
 		$this->hierarchy = new LoggerHierarchy(new LoggerRoot());
 	}
 	
-	public function testIfLevelIsInitiallyLevelAllg() {
-		self::assertEquals('ALL', $this->hierarchy->getRootLogger()->getLevel()->toString());
-	}
-
-	public function testIfNameIsRoot() {
-		self::assertEquals('root', $this->hierarchy->getRootLogger()->getName());
-	}
-
-	public function testIfParentIsNull() {
-		self::assertSame(null, $this->hierarchy->getRootLogger()->getParent());
-	}
-
-	public function testSetParent() {
-		$l = $this->hierarchy->getLogger('dummy');
-		$this->hierarchy->getRootLogger()->setParent($l);
-		$this->testIfParentIsNull();
-	}
-        
 	public function testResetConfiguration() {
 		$root = $this->hierarchy->getRootLogger();
 		$appender = new LoggerAppenderConsole('A1');
 		$root->addAppender($appender);
+
 		$logger = $this->hierarchy->getLogger('test');
-		self::assertEquals(count($this->hierarchy->getCurrentLoggers()), 1);
+		self::assertEquals(1, count($this->hierarchy->getCurrentLoggers()));
+		
 		$this->hierarchy->resetConfiguration();
-		self::assertEquals($this->hierarchy->getRootLogger()->getLevel()->toString(), 'DEBUG');
-		self::assertEquals($this->hierarchy->getThreshold()->toString(), 'ALL');
-		self::assertEquals(count($this->hierarchy->getCurrentLoggers()), 1);
-		foreach($this->hierarchy->getCurrentLoggers() as $l) {
-			self::assertEquals($l->getLevel(), null);
-			self::assertTrue($l->getAdditivity());
-			self::assertEquals(count($l->getAllAppenders()), 0);
+		self::assertEquals(LoggerLevel::getLevelDebug(), $root->getLevel());
+		self::assertEquals(LoggerLevel::getLevelAll(), $this->hierarchy->getThreshold());
+		self::assertEquals(1, count($this->hierarchy->getCurrentLoggers()));
+		
+		foreach($this->hierarchy->getCurrentLoggers() as $logger) {
+			self::assertNull($logger->getLevel());
+			self::assertTrue($logger->getAdditivity());
+			self::assertEquals(0, count($logger->getAllAppenders()), 0);
 		}
 	}
 	

Modified: logging/log4php/trunk/src/test/php/LoggerRootTest.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/LoggerRootTest.php?rev=1343190&r1=1343189&r2=1343190&view=diff
==============================================================================
--- logging/log4php/trunk/src/test/php/LoggerRootTest.php (original)
+++ logging/log4php/trunk/src/test/php/LoggerRootTest.php Mon May 28 10:18:49 2012
@@ -26,30 +26,38 @@
  * @group main
  */
 class LoggerRootTest extends PHPUnit_Framework_TestCase {
-        
-	private $loggerRoot;
-        
-	protected function setUp() {
-		$this->loggerRoot = new LoggerRoot();
-	}
-        
-	public function testIfLevelIsInitiallyLevelAll() {
-		self::assertEquals($this->loggerRoot->getLevel()->toString(), 'ALL');
+    
+	public function testInitialSetup() {
+		$root = new LoggerRoot();
+		self::assertSame(LoggerLevel::getLevelAll(), $root->getLevel());
+		self::assertSame(LoggerLevel::getLevelAll(), $root->getEffectiveLevel());
+		self::assertSame('root', $root->getName());
+		self::assertNull($root->getParent());
 	}
 
-	public function testIfNameIsRoot() {
-		self::assertEquals($this->loggerRoot->getName(), 'root');
+	/**
+	 * @expectedException PHPUnit_Framework_Error
+	 * @expectedExceptionMessage log4php: LoggerRoot cannot have a parent.
+	 */
+	public function testSetParentWarning() {
+		$root = new LoggerRoot();
+		$logger = new Logger('test');
+		$root->setParent($logger);
 	}
-
-	public function testIfParentIsNull() {
-		self::assertSame($this->loggerRoot->getParent(), null);
+	
+	public function testSetParentResult() {
+		$root = new LoggerRoot();
+		$logger = new Logger('test');
+		@$root->setParent($logger);
+		self::assertNull($root->getParent());
 	}
-
-	public function testSetParent() {
-		$hierarchy = new LoggerHierarchy(new LoggerRoot());
-		$l = $hierarchy->getLogger('dummy');
-		$this->loggerRoot->setParent($l);
-		$this->testIfParentIsNull();
+	
+	/**
+	 * @expectedException PHPUnit_Framework_Error
+	 * @expectedExceptionMessage log4php: Cannot set LoggerRoot level to null.
+	 */
+	public function testNullLevelWarning() {
+		$root = new LoggerRoot();
+		$root->setLevel(null);
 	}
-
 }