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/10/07 10:28:54 UTC

svn commit: r1395241 - in /logging/log4php/trunk/src: changes/changes.xml main/php/Logger.php test/php/LoggerTest.php test/php/LoggerTest.properties

Author: ihabunek
Date: Sun Oct  7 08:28:53 2012
New Revision: 1395241

URL: http://svn.apache.org/viewvc?rev=1395241&view=rev
Log:
LOG4PHP-188: Events logged by upstream loggers even if disabled by level.

Removed:
    logging/log4php/trunk/src/test/php/LoggerTest.properties
Modified:
    logging/log4php/trunk/src/changes/changes.xml
    logging/log4php/trunk/src/main/php/Logger.php
    logging/log4php/trunk/src/test/php/LoggerTest.php

Modified: logging/log4php/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/changes/changes.xml?rev=1395241&r1=1395240&r2=1395241&view=diff
==============================================================================
--- logging/log4php/trunk/src/changes/changes.xml (original)
+++ logging/log4php/trunk/src/changes/changes.xml Sun Oct  7 08:28:53 2012
@@ -21,6 +21,7 @@
 	</properties>
 	<body>
 		<release version="2.3.0" date="SVN">
+			<action date="2012-10-07" type="fix" issue="LOG4PHP-188" dev="Ivan Habunek">Events logged by upstream loggers even if disabled by level.</action>
 			<action date="2012-10-06" type="update" issue="LOG4PHP-186" dev="Ivan Habunek" due-to="Rasmus Lerdorf" due-to-email="rasmus at lerdorf dot com">Don't clear the entire stat cache on an append.</action>
 			<action date="2012-10-06" type="add" issue="LOG4PHP-141" dev="Ivan Habunek">Allow setting of a default renderer.</action>
 			<action date="2012-09-08" type="update" issue="LOG4PHP-120" dev="Ivan Habunek" due-to="Michal Vanek" due-to-email="michal dot vanek at gmail dot com">Fixed LoggerAppenderDailyFile to rollover on date change in long running scipts.</action>

Modified: logging/log4php/trunk/src/main/php/Logger.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/Logger.php?rev=1395241&r1=1395240&r2=1395241&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/Logger.php (original)
+++ logging/log4php/trunk/src/main/php/Logger.php Sun Oct  7 08:28:53 2012
@@ -170,7 +170,34 @@ class Logger {
 	 */
 	public function log(LoggerLevel $level, $message, $throwable = null) {
 		if($this->isEnabledFor($level)) {
-			$this->forcedLog($this->fqcn, $throwable, $level, $message);
+			$event = new LoggerLoggingEvent($this->fqcn, $this, $level, $message, null, $throwable);
+			$this->callAppenders($event);
+		}
+		
+		// Forward the event upstream if additivity is turned on
+		if(isset($this->parent) && $this->getAdditivity()) {
+			
+			// Use the event if already created
+			if (isset($event)) {
+				$this->parent->logEvent($event);
+			} else {
+				$this->parent->log($level, $message, $throwable);
+			}
+		}
+	}
+	
+	/**
+	 * Logs an already prepared logging event object. 
+	 * @param LoggerLoggingEvent $event
+	 */
+	public function logEvent(LoggerLoggingEvent $event) {
+		if($this->isEnabledFor($event->getLevel())) {
+			$this->callAppenders($event);
+		}
+		
+		// Forward the event upstream if additivity is turned on
+		if(isset($this->parent) && $this->getAdditivity()) {
+			$this->parent->logEvent($event);
 		}
 	}
 	
@@ -202,8 +229,24 @@ class Logger {
 	 * @param mixed $message message to log
 	 */
 	public function forcedLog($fqcn, $throwable, LoggerLevel $level, $message) {
-		$this->callAppenders(new LoggerLoggingEvent($fqcn, $this, $level, $message, null, $throwable));
-	} 
+		$event = new LoggerLoggingEvent($fqcn, $this, $level, $message, null, $throwable);
+		$this->callAppenders($event);
+		
+		// Forward the event upstream if additivity is turned on
+		if(isset($this->parent) && $this->getAdditivity()) {
+			$this->parent->logEvent($event);
+		}
+	}
+
+	/**
+	 * Forwards the given logging event to all linked appenders.
+	 * @param LoggerLoggingEvent $event
+	 */
+	public function callAppenders($event) {
+		foreach($this->appenders as $appender) {
+			$appender->doAppend($event);
+		}
+	}
 	
 	// ******************************************
 	// *** Checker methods                    ***
@@ -302,22 +345,6 @@ class Logger {
 	}
 	
 	/**
-	 * Forwards the given logging event to all linked appenders.
-	 * @param LoggerLoggingEvent $event 
-	 */
-	public function callAppenders($event) {
-		// Forward the event to each linked appender
-		foreach($this->appenders as $appender) {
-			$appender->doAppend($event);
-		}
-		
-		// Forward the event upstream if additivity is turned on
-		if(isset($this->parent) && $this->getAdditivity()) {
-			$this->parent->callAppenders($event);
-		}
-	}
-	
-	/**
 	 * Returns the appenders linked to this logger as an array.
 	 * @return array collection of appender names
 	 */

Modified: logging/log4php/trunk/src/test/php/LoggerTest.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/LoggerTest.php?rev=1395241&r1=1395240&r2=1395241&view=diff
==============================================================================
--- logging/log4php/trunk/src/test/php/LoggerTest.php (original)
+++ logging/log4php/trunk/src/test/php/LoggerTest.php Sun Oct  7 08:28:53 2012
@@ -27,6 +27,88 @@
  */
 class LoggerTest extends PHPUnit_Framework_TestCase {
 	
+	private $testConfig1 = array (
+		'rootLogger' =>	array (
+			'level' => 'ERROR',
+			'appenders' => array (
+				'default',
+			),
+		),
+		'appenders' => array (
+			'default' => array (
+				'class' => 'LoggerAppenderEcho',
+			),
+		),
+		'loggers' => array (
+			'mylogger' => array (
+				'additivity' => 'false',
+				'level' => 'DEBUG',
+				'appenders' => array (
+					'default',
+				),
+			),
+		),
+	);
+	
+	// For testing additivity
+	private $testConfig2 = array (
+		'appenders' => array (
+			'default' => array (
+				'class' => 'LoggerAppenderEcho',
+			),
+		),
+		'rootLogger' => array(
+			'appenders' => array('default'),
+		),
+		'loggers' => array (
+			'foo' => array (
+				'appenders' => array (
+					'default',
+				),
+			),
+			'foo.bar' => array (
+				'appenders' => array (
+					'default',
+				),
+			),
+			'foo.bar.baz' => array (
+				'appenders' => array (
+					'default',
+				),
+			),
+		),
+	);
+	
+	// For testing additivity
+	private $testConfig3 = array (
+		'appenders' => array (
+			'default' => array (
+				'class' => 'LoggerAppenderEcho',
+			),
+		),
+		'rootLogger' => array(
+			'appenders' => array('default'),
+		),
+		'loggers' => array (
+			'foo' => array (
+				'appenders' => array (
+					'default',
+				),
+			),
+			'foo.bar' => array (
+				'appenders' => array (
+					'default',
+				),
+			),
+			'foo.bar.baz' => array (
+				'level' => 'ERROR',
+				'appenders' => array (
+					'default',
+				),
+			),
+		),
+	);
+	
 	protected function setUp() {
 		Logger::clear();
 		Logger::resetConfiguration();
@@ -54,7 +136,7 @@ class LoggerTest extends PHPUnit_Framewo
 	}
 	
 	public function testCanLogToAllLevels() {
-		Logger::configure(dirname(__FILE__) . '/LoggerTest.properties');
+		Logger::configure($this->testConfig1);
 		
 		$logger = Logger::getLogger('mylogger');
 		ob_start();
@@ -63,7 +145,6 @@ class LoggerTest extends PHPUnit_Framewo
 		$logger->error('this is an error');
 		$logger->debug('this is a debug message');
 		$logger->fatal('this is a fatal message');
-		
 		$v = ob_get_contents();
 		ob_end_clean();
 		
@@ -77,17 +158,25 @@ class LoggerTest extends PHPUnit_Framewo
 	}
 	
 	public function testIsEnabledFor() {
-		Logger::configure(dirname(__FILE__) . '/LoggerTest.properties');
+		Logger::configure($this->testConfig1);
 		
 		$logger = Logger::getLogger('mylogger');
 		
+		self::assertFalse($logger->isTraceEnabled());
 		self::assertTrue($logger->isDebugEnabled());
 		self::assertTrue($logger->isInfoEnabled());
+		self::assertTrue($logger->isWarnEnabled());
+		self::assertTrue($logger->isErrorEnabled());
+		self::assertTrue($logger->isFatalEnabled());
 		
 		$logger = Logger::getRootLogger();
 		
+		self::assertFalse($logger->isTraceEnabled());
 		self::assertFalse($logger->isDebugEnabled());
 		self::assertFalse($logger->isInfoEnabled());
+		self::assertFalse($logger->isWarnEnabled());
+		self::assertTrue($logger->isErrorEnabled());
+		self::assertTrue($logger->isFatalEnabled());
 	}
 	
 	public function testGetCurrentLoggers() {
@@ -96,10 +185,39 @@ class LoggerTest extends PHPUnit_Framewo
 		
 		self::assertEquals(0, count(Logger::getCurrentLoggers()));
 		
-		Logger::configure(dirname(__FILE__) . '/LoggerTest.properties');
+		Logger::configure($this->testConfig1);
 		self::assertEquals(1, count(Logger::getCurrentLoggers()));
 		$list = Logger::getCurrentLoggers();
 		self::assertEquals('mylogger', $list[0]->getName());
 	}
-
+	
+	public function testAdditivity() {
+		Logger::configure($this->testConfig2);
+	
+		$logger = Logger::getLogger('foo.bar.baz');
+		ob_start();
+		$logger->info('test');
+		$actual = ob_get_contents();
+		ob_end_clean();
+	
+		// The message should get logged 4 times: once by every logger in the 
+		//  hierarchy (including root)
+		$expected = str_repeat('INFO - test' . PHP_EOL, 4);
+		self::assertSame($expected, $actual);
+	}
+	
+	public function testAdditivity2() {
+		Logger::configure($this->testConfig3);
+	
+		$logger = Logger::getLogger('foo.bar.baz');
+		ob_start();
+		$logger->info('test');
+		$actual = ob_get_contents();
+		ob_end_clean();
+	
+		// The message should get logged 3 times: once by every logger in the
+		//  hierarchy, except foo.bar.baz which is set to level ERROR
+		$expected = str_repeat('INFO - test' . PHP_EOL, 3);
+		self::assertSame($expected, $actual);
+	}
 }