logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ihabu...@apache.org
Subject svn commit: r1173515 - in /logging/log4php/branches/experimental/config-adapters/src: main/php/ test/php/ test/php/filters/
Date Wed, 21 Sep 2011 06:59:20 GMT
Author: ihabunek
Date: Wed Sep 21 06:59:19 2011
New Revision: 1173515

URL: http://svn.apache.org/viewvc?rev=1173515&view=rev
Log:
More tests. Added test helper class.

Added:
    logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerTestHelper.php
Modified:
    logging/log4php/branches/experimental/config-adapters/src/main/php/LoggerConfigurator.php
    logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerConfiguratorTest.php
    logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterDenyAllTest.php
    logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterLevelMatchTest.php
    logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterStringMatchTest.php

Modified: logging/log4php/branches/experimental/config-adapters/src/main/php/LoggerConfigurator.php
URL: http://svn.apache.org/viewvc/logging/log4php/branches/experimental/config-adapters/src/main/php/LoggerConfigurator.php?rev=1173515&r1=1173514&r2=1173515&view=diff
==============================================================================
--- logging/log4php/branches/experimental/config-adapters/src/main/php/LoggerConfigurator.php
(original)
+++ logging/log4php/branches/experimental/config-adapters/src/main/php/LoggerConfigurator.php
Wed Sep 21 06:59:19 2011
@@ -45,10 +45,10 @@ class LoggerConfigurator
 	);
 	
 	/** Default configuration; used if no configuration file is provided. */
-	private $defaultConfiguration = array(
+	private static $defaultConfiguration = array(
         'threshold' => 'ALL',
         'rootLogger' => array(
-            'level' => 'INFO',
+            'level' => 'DEBUG',
             'appenders' => array('default'),
         ),
         'appenders' => array(
@@ -80,8 +80,7 @@ class LoggerConfigurator
 	 * 		configuration as an array. If not set, default configuration 
 	 * 		will be used.
 	 */
-	public function configure(LoggerHierarchy $hierarchy, $input = null)
-	{
+	public function configure(LoggerHierarchy $hierarchy, $input = null) {
 		$config = $this->parse($input);
 		$this->doConfigure($hierarchy, $config);
 	}
@@ -103,7 +102,7 @@ class LoggerConfigurator
 	{
 		// No input - use default configuration
 		if (!isset($input)) {
-			$config =  $this->defaultConfiguration;
+			$config = self::$defaultConfiguration;
 		}
 		
 		// Array input - contains configuration within the array
@@ -117,14 +116,14 @@ class LoggerConfigurator
 				$config = $this->parseFile($input);
 			} catch (LoggerException $e) {
 				$this->warn("Configuration failed. " . $e->getMessage() . " Using default configuration.");
-				$config = $this->defaultConfiguration;
+				$config = self::$defaultConfiguration;
 			}
 		}
 		
 		// Anything else is an error
 		else {
 			$this->warn("Invalid configuration param given. Reverting to default configuration.");
-			$config = $this->defaultConfiguration;
+			$config = self::$defaultConfiguration;
 		}
 		
 		return $config;
@@ -134,8 +133,8 @@ class LoggerConfigurator
 	 * Returns the default log4php configuration.
 	 * @return array
 	 */
-	public function getDefaultConfiguration() {
-		return $this->defaultConfiguration;
+	public static function getDefaultConfiguration() {
+		return self::$defaultConfiguration;
 	} 
 	
 	/**
@@ -262,6 +261,13 @@ class LoggerConfigurator
 			$this->createAppenderLayout($appender, $config['layout']);
 		}
 		
+		// Parse filters
+		if (isset($config['filters']) && is_array($config['filters'])) {
+			foreach($config['filters'] as $filterConfig) {
+				$this->createAppenderFilter($appender, $filterConfig);
+			}
+		}
+		
 		// Set options if any
 		if (isset($config['params'])) {
 			$this->setOptions($appender, $config['params']);
@@ -275,7 +281,7 @@ class LoggerConfigurator
 	/**
 	 * Parses layout config, creates the layout and links it to the appender.
 	 * @param LoggerAppender $appender
-	 * @param array $config Layout configuration options.
+	 * @param array $config Layout configuration.
 	 */
 	private function createAppenderLayout(LoggerAppender $appender, $config) {
 		$name = $appender->getName();
@@ -285,7 +291,7 @@ class LoggerConfigurator
 			return;
 		}
 		
-		$layout = new $class;
+		$layout = new $class();
 		if (!($layout instanceof LoggerLayout)) {
 			$this->warn("Invalid layout class [$class] sepcified for appender [$name]. Reverting
to default layout.");
 			return;
@@ -296,10 +302,37 @@ class LoggerConfigurator
 		}
 		
 		$layout->activateOptions();
-		
 		$appender->setLayout($layout);
 	}
 	
+	/**
+	 * Parses filter config, creates the filter and adds it to the appender's 
+	 * filter chain.
+	 * @param LoggerAppender $appender
+	 * @param array $config Filter configuration.
+	 */
+	private function createAppenderFilter(LoggerAppender $appender, $config) {
+		$name = $appender->getName();
+		$class = $config['class'];
+		if (!class_exists($class)) {
+			$this->warn("Nonexistant filter class [$class] specified on appender [$name]. Skipping
filter definition.");
+			return;
+		}
+	
+		$filter = new $class();
+		if (!($filter instanceof LoggerFilter)) {
+			$this->warn("Invalid filter class [$class] sepcified on appender [$name]. Skipping
filter definition.");
+			return;
+		}
+	
+		if (isset($config['params'])) {
+			$this->setOptions($filter, $config['params']);
+		}
+	
+		$filter->activateOptions();
+		$appender->addFilter($filter);
+	}
+	
 	/** 
 	 * Configures the root logger
 	 * @see configureLogger() 

Modified: logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerConfiguratorTest.php
URL: http://svn.apache.org/viewvc/logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerConfiguratorTest.php?rev=1173515&r1=1173514&r2=1173515&view=diff
==============================================================================
--- logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerConfiguratorTest.php
(original)
+++ logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerConfiguratorTest.php
Wed Sep 21 06:59:19 2011
@@ -17,9 +17,8 @@
  		Logger::resetConfiguration();
  	}
  	
- 	/** Check proper default setup. */
+ 	/** Check default setup. */
  	public function testDefaultConfig() {
- 		// Uses default config file
  		Logger::configure();
  		
  		$actual = Logger::getCurrentLoggers();
@@ -79,7 +78,9 @@
 		 		 	'class' => 'LoggerAppenderEcho',
 		 		 	'layout' => array(
 		 		 		'class' => 'LoggerLayoutPattern',
-		 		 		'conversionPattern' => 'message: %m%n'
+		 		 		'params' => array(
+		 		 			'conversionPattern' => 'message: %m%n'
+		 		 		)
 		 			)
 		 		),
  			) 

Added: logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerTestHelper.php
URL: http://svn.apache.org/viewvc/logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerTestHelper.php?rev=1173515&view=auto
==============================================================================
--- logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerTestHelper.php
(added)
+++ logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerTestHelper.php
Wed Sep 21 06:59:19 2011
@@ -0,0 +1,47 @@
+<?php
+
+/** A set of helper functions for running tests. */
+class LoggerTestHelper {
+	
+	public static function getTraceEvent($message = 'test') {
+		return new LoggerLoggingEvent(__CLASS__, new Logger("test"), LoggerLevel::getLevelTrace(),
$message);
+	}
+	
+	public static function getDebugEvent($message = 'test') {
+		return new LoggerLoggingEvent(__CLASS__, new Logger("test"), LoggerLevel::getLevelDebug(),
$message);
+	}
+	
+	public static function getInfoEvent($message = 'test') {
+		return new LoggerLoggingEvent(__CLASS__, new Logger("test"), LoggerLevel::getLevelInfo(),
$message);
+	}
+	
+	public static function getWarnEvent($message = 'test') {
+		return new LoggerLoggingEvent(__CLASS__, new Logger("test"), LoggerLevel::getLevelWarn(),
$message);
+	}
+	
+	public static function getErrorEvent($message = 'test') {
+		return new LoggerLoggingEvent(__CLASS__, new Logger("test"), LoggerLevel::getLevelError(),
$message);
+	}
+	
+	public static function getFatalEvent($message = 'test') {
+		return new LoggerLoggingEvent(__CLASS__, new Logger("test"), LoggerLevel::getLevelFatal(),
$message);
+	}
+	
+	public static function getAllEvents($message = 'test') {
+		return array(
+			self::getTraceEvent($message),
+			self::getDebugEvent($message),
+			self::getInfoEvent($message),
+			self::getWarnEvent($message),
+			self::getErrorEvent($message),
+			self::getFatalEvent($message),
+		);
+	}
+}
+
+
+
+
+
+
+?>
\ No newline at end of file

Modified: logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterDenyAllTest.php
URL: http://svn.apache.org/viewvc/logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterDenyAllTest.php?rev=1173515&r1=1173514&r2=1173515&view=diff
==============================================================================
--- logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterDenyAllTest.php
(original)
+++ logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterDenyAllTest.php
Wed Sep 21 06:59:19 2011
@@ -31,17 +31,41 @@ class LoggerFilterDenyAllTest extends PH
 	public function testDecide() {
 		$filter = new LoggerFilterDenyAll();
 		
-		$eventError = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelError(),
"testmessage");
-		$eventDebug = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelDebug(),
"testmessage");
-		$eventWarn = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelWarn(),
"testmessage");
+		$events = array(
+			LoggerTestHelper::getTraceEvent(),
+			LoggerTestHelper::getDebugEvent(),
+			LoggerTestHelper::getInfoEvent(),
+			LoggerTestHelper::getWarnEvent(),
+			LoggerTestHelper::getErrorEvent(),
+			LoggerTestHelper::getFatalEvent(),
+		);
 		
-		$result = $filter->decide($eventError);
-		self::assertEquals($result, LoggerFilter::DENY);
-		
-		$result = $filter->decide($eventDebug);
-		self::assertEquals($result, LoggerFilter::DENY);
-		
-		$result = $filter->decide($eventWarn);
-		self::assertEquals($result, LoggerFilter::DENY);
+		foreach($events as $event) {
+			$actual = $filter->decide($event);
+			self::assertEquals(LoggerFilter::DENY, $actual);
+		}
+    }
+    
+    public function testConfiguration() {
+    	$config = LoggerConfigurator::getDefaultConfiguration();
+    	$config['appenders']['default']['filters'] = array(
+    		array(
+    			'class' => 'LoggerFilterDenyAll'
+    		)
+    	);
+    	
+    	Logger::configure($config);
+    	$logger = Logger::getRootLogger();
+    	
+    	ob_start();
+    	$logger->trace('Test');
+    	$logger->debug('Test');
+    	$logger->info('Test');
+    	$logger->warn('Test');
+    	$logger->error('Test');
+    	$logger->fatal('Test');
+    	$actual = ob_get_clean();
+    	
+    	$this->assertEmpty($actual);
     }
 }

Modified: logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterLevelMatchTest.php
URL: http://svn.apache.org/viewvc/logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterLevelMatchTest.php?rev=1173515&r1=1173514&r2=1173515&view=diff
==============================================================================
--- logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterLevelMatchTest.php
(original)
+++ logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterLevelMatchTest.php
Wed Sep 21 06:59:19 2011
@@ -27,7 +27,7 @@
  * @group filters
  */
 class LoggerFilterLevelMatchTest extends PHPUnit_Framework_TestCase {
-        
+		
 	public function testDecideAcceptErrorLevel() {
 		$filter = new LoggerFilterLevelMatch();
 		$filter->setAcceptOnMatch(true);
@@ -45,9 +45,9 @@ class LoggerFilterLevelMatchTest extends
 		
 		$result = $filter->decide($eventWarn);
 		self::assertEquals($result, LoggerFilter::NEUTRAL);
-    }
-    
-    public function testDecideDenyErrorLevel() {
+	}
+	
+	public function testDecideDenyErrorLevel() {
 		$filter = new LoggerFilterLevelMatch();
 		$filter->setAcceptOnMatch("false");
 		$filter->setLevelToMatch(LoggerLevel::getLevelError());
@@ -64,9 +64,9 @@ class LoggerFilterLevelMatchTest extends
 		
 		$result = $filter->decide($eventWarn);
 		self::assertEquals($result, LoggerFilter::NEUTRAL);
-    }
-    
-    public function testDecideAcceptWarnLevel() {
+	}
+	
+	public function testDecideAcceptWarnLevel() {
 		$filter = new LoggerFilterLevelMatch();
 		$filter->setAcceptOnMatch("true");
 		$filter->setLevelToMatch(LoggerLevel::getLevelWarn());
@@ -83,9 +83,9 @@ class LoggerFilterLevelMatchTest extends
 		
 		$result = $filter->decide($eventWarn);
 		self::assertEquals($result, LoggerFilter::ACCEPT);
-    }
-    
-    public function testDecideDenyWarnLevel() {
+	}
+	
+	public function testDecideDenyWarnLevel() {
 		$filter = new LoggerFilterLevelMatch();
 		$filter->setAcceptOnMatch("false");
 		$filter->setLevelToMatch(LoggerLevel::getLevelWarn());
@@ -102,9 +102,9 @@ class LoggerFilterLevelMatchTest extends
 		
 		$result = $filter->decide($eventWarn);
 		self::assertEquals($result, LoggerFilter::DENY);
-    }
-    
-    public function testDecideDenyDebugLevel() {
+	}
+	
+	public function testDecideDenyDebugLevel() {
 		$filter = new LoggerFilterLevelMatch();
 		$filter->setAcceptOnMatch("false");
 		$filter->setLevelToMatch(LoggerLevel::getLevelDebug());
@@ -121,5 +121,42 @@ class LoggerFilterLevelMatchTest extends
 		
 		$result = $filter->decide($eventWarn);
 		self::assertEquals($result, LoggerFilter::NEUTRAL);
-    }
+	}
+	
+	public function testConfiguration() {
+		$config = LoggerConfigurator::getDefaultConfiguration();
+		
+		// Add filters to accept debug and deny all others
+		$config['appenders']['default']['filters'] = array(
+			array(
+				'class' => 'LoggerFilterLevelMatch',
+				'params' => array(
+					'levelToMatch' => 'error',
+					'acceptOnMatch' => true
+				)
+			),
+			array(
+				'class' => 'LoggerFilterDenyAll',
+			)
+		);
+		 
+		Logger::configure($config);
+		$logger = Logger::getRootLogger();
+		 
+		ob_start();
+		$logger->trace('Test');
+		$logger->debug('Test');
+		$logger->info('Test');
+		$logger->warn('Test');
+		$logger->error('Test');
+		$logger->fatal('Test');
+		$actual = ob_get_clean();
+		
+		var_dump($actual);
+		
+		$this->assertEmpty($actual);
+	}
+	
+	
+	
 }

Modified: logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterStringMatchTest.php
URL: http://svn.apache.org/viewvc/logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterStringMatchTest.php?rev=1173515&r1=1173514&r2=1173515&view=diff
==============================================================================
--- logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterStringMatchTest.php
(original)
+++ logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterStringMatchTest.php
Wed Sep 21 06:59:19 2011
@@ -23,6 +23,8 @@
  * @link       http://logging.apache.org/log4php
  */
 
+require dirname(__FILE__) . '/../LoggerTestHelper.php';
+
 /**
  * @group filters
  */



Mime
View raw message