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/08 10:48:04 UTC

svn commit: r1395470 - in /logging/log4php/trunk/src: main/php/layouts/LoggerLayoutPattern.php main/php/pattern/LoggerPatternConverterThrowable.php test/php/pattern/LoggerPatternConverterTest.php

Author: ihabunek
Date: Mon Oct  8 08:48:03 2012
New Revision: 1395470

URL: http://svn.apache.org/viewvc?rev=1395470&view=rev
Log:
LoggerLayoutPattern:
- greatly simplified rendering of exceptions
- added %exception specifier, along with %ex and %throwable
- more tests

Modified:
    logging/log4php/trunk/src/main/php/layouts/LoggerLayoutPattern.php
    logging/log4php/trunk/src/main/php/pattern/LoggerPatternConverterThrowable.php
    logging/log4php/trunk/src/test/php/pattern/LoggerPatternConverterTest.php

Modified: logging/log4php/trunk/src/main/php/layouts/LoggerLayoutPattern.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/layouts/LoggerLayoutPattern.php?rev=1395470&r1=1395469&r2=1395470&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/layouts/LoggerLayoutPattern.php (original)
+++ logging/log4php/trunk/src/main/php/layouts/LoggerLayoutPattern.php Mon Oct  8 08:48:03 2012
@@ -59,6 +59,7 @@ class LoggerLayoutPattern extends Logger
 		'env' => 'LoggerPatternConverterEnvironment',
 		
 		'ex' => 'LoggerPatternConverterThrowable',
+		'exception' => 'LoggerPatternConverterThrowable',
 		'throwable' => 'LoggerPatternConverterThrowable',
 		
 		'F' => 'LoggerPatternConverterFile',

Modified: logging/log4php/trunk/src/main/php/pattern/LoggerPatternConverterThrowable.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/pattern/LoggerPatternConverterThrowable.php?rev=1395470&r1=1395469&r2=1395470&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/pattern/LoggerPatternConverterThrowable.php (original)
+++ logging/log4php/trunk/src/main/php/pattern/LoggerPatternConverterThrowable.php Mon Oct  8 08:48:03 2012
@@ -21,50 +21,20 @@
 /**
  * Returns the throwable information linked to the logging event, if any.
  * 
- * Option: the maximum stack trace lines to return (returns all if not set)
- * 
  * @package log4php
  * @subpackage pattern
  * @version $Revision$
  * @since 2.3
  */
 class LoggerPatternConverterThrowable extends LoggerPatternConverter {
-	
-	private $depth;
-	
-	public function activateOptions() {
-		if (isset($this->option) && is_numeric($op) && $op >= 0) {
-			$this->depth = (integer) $this->option;
-		}
-	}
-	
+
 	public function convert(LoggerLoggingEvent $event) {
-		
 		$info = $event->getThrowableInformation();
-		if ($info === null) {
-			return '';
-		}
-		
-		$ex = $info->getThrowable();
-		
-		// Format exception to string
-		$strEx = get_class($ex) . ': "' . $ex->getMessage() . '"' . PHP_EOL;
-		$strEx .= 'at '. $ex->getFile() . ':' . $ex->getLine();
-		
-		// Add trace if required
-		if ($this->depth === null || $this->depth > 0) {
-			$trace = $ex->getTrace();
-			foreach($trace as $key => $item) {
-				if (isset($this->depth) && $key > $this->depth) {
-					break;
-				}
-				$strEx .= PHP_EOL . "#$key " . 
-					"{$item['file']}:{$item['line']} " .
-					"in {$item['class']}{$item['type']}{$item['function']}()"; 
-			}
+		if (isset($info)) {
+			$ex = $info->getThrowable();
+			return (string) $ex . PHP_EOL;
 		}
-		
-		return $strEx;
+		return '';
 	}
 }
  
\ No newline at end of file

Modified: logging/log4php/trunk/src/test/php/pattern/LoggerPatternConverterTest.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/pattern/LoggerPatternConverterTest.php?rev=1395470&r1=1395469&r2=1395470&view=diff
==============================================================================
--- logging/log4php/trunk/src/test/php/pattern/LoggerPatternConverterTest.php (original)
+++ logging/log4php/trunk/src/test/php/pattern/LoggerPatternConverterTest.php Mon Oct  8 08:48:03 2012
@@ -17,7 +17,7 @@
  *
  * @category   tests
  * @package    log4php
- * @subpackage filters
+ * @subpackage pattern
  * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  * @version    $Revision$
  * @link       http://logging.apache.org/log4php
@@ -173,6 +173,28 @@ class LoggerPatternConverterTest extends
 
 		Logger::resetConfiguration();
 	}
+	
+	public function testLocation2() {
+		$config = LoggerTestHelper::getEchoPatternConfig("%location");
+		Logger::configure($config);
+	
+		// Test by capturing output. Logging methods of a Logger object must
+		// be used for the location info to be formed correctly.
+		ob_start();
+		$log = Logger::getLogger('foo');
+		$log->info('foo'); $line = __LINE__; // Do NOT move this to next line.
+		$actual = ob_get_contents();
+		ob_end_clean();
+	
+		$class = __CLASS__;
+		$func = __FUNCTION__;
+		$file = __FILE__;
+		
+		$expected = "$class.$func($file:$line)";
+		self::assertSame($expected, $actual);
+	
+		Logger::resetConfiguration();
+	}
 
 	public function testMessage() {
 		$expected = "This is a message.";