LOG4PHP-110 Added first part from Vladimir Gorej which improves the exception handlin...
authorChristian Grobmeier <grobmeier@apache.org>
Fri, 18 Jun 2010 04:55:34 +0000 (04:55 +0000)
committerChristian Grobmeier <grobmeier@apache.org>
Fri, 18 Jun 2010 04:55:34 +0000 (04:55 +0000)
git-svn-id: https://svn.apache.org/repos/asf/logging/log4php/trunk@955838 13f79535-47bb-0310-9956-ffa450edef68

src/main/php/Logger.php
src/main/php/LoggerLoggingEvent.php
src/main/php/LoggerThrowableInformation.php
src/main/php/renderers/LoggerRendererException.php
src/test/php/LoggerLoggingEventTest.php
src/test/php/LoggerThrowableInformationTest.php
src/test/php/renderers/LoggerRendererExceptionTest.php

index a448ec6..8ee81ee 100644 (file)
@@ -282,10 +282,7 @@ class Logger {
         * @see LoggerLoggingEvent                      
         */
        public function forcedLog($fqcn, $caller, $level, $message) {
-               $throwable = null;
-               if ($caller !== null && $caller instanceof Exception) {
-                       $throwable = $caller;                                                    
-               }
+               $throwable = ($caller !== null && $caller instanceof Exception) ? $caller : null;
                
                $this->callAppenders(new LoggerLoggingEvent($fqcn, $this, $level, $message, null, $throwable));
        } 
index 20cfe60..56161d5 100644 (file)
@@ -128,7 +128,7 @@ class LoggerLoggingEvent {
        * @param integer $timeStamp the timestamp of this logging event.
     * @param Exception $throwable The throwable associated with logging event
        */
-       public function __construct($fqcn, $logger, $priority, $message, $timeStamp = null, $throwable = null) {
+       public function __construct($fqcn, $logger, $priority, $message, $timeStamp = null, Exception $throwable = null) {
                $this->fqcn = $fqcn;
                if($logger instanceof Logger) {
                        $this->logger = $logger;
index 7960da3..75511c9 100644 (file)
@@ -38,16 +38,20 @@ class LoggerThrowableInformation {
        /**
         * Create a new instance
         * 
-        * @param $throwable - a throwable either as array or as a exception
+        * @param $throwable - a throwable as a exception
+        * @param $logger - Logger reference
         */
-       public function __construct($throwable)  {
-               if(is_array($throwable)) {
-                   $this->throwableArray = $throwable;
-               } else if($throwable instanceof Exception) {
-                   $this->throwable = $throwable;
-               } else {
-                   throw new InvalidArgumentException();
-               }
+       public function __construct(Exception $throwable)  {
+               $this->throwable = $throwable;
+       }
+       
+       /**
+       * Return source exception
+       * 
+       * @return Exception
+       */
+       public function getThrowable() {
+               return $this->throwable;
        }
        
        /**
@@ -56,16 +60,14 @@ class LoggerThrowableInformation {
         * @return array 
         */
        public function getStringRepresentation() {
-               if (!is_array($this->throwableArray) && $this->throwable !== null) {
-                       $this->throwableArray = array();
-                       $ex     = $this->throwable;
-                       $this->throwableArray[] = $ex->getMessage();
-                       while (method_exists($ex, 'getPrevious')) {
-                               $ex     = $ex->getPrevious();
-                               if ($ex !== null && $ex instanceof Exception) {
-                                       $this->throwableArray[] = $ex->getMessage();
-                               }
+               if (!is_array($this->throwableArray)) {
+                       $renderer = Logger::getHierarchy()->getRendererMap()->getByClassName(get_class($this->throwable));
+                       
+                       // TODO: why this?
+                       if ($renderer instanceof LoggerRendererDefault) {
+                               $renderer = new LoggerRendererException();
                        }
+                       $this->throwableArray = explode("\n", $renderer->render($this->throwable));
                }
                
                return $this->throwableArray;
index c4b3614..63989f0 100644 (file)
 class LoggerRendererException implements LoggerRendererObject {
 
        public function render($o) {
-               $ex     = $o;
-               $fullTrace = $this->getExceptionAsString($ex);
-               while (method_exists($ex, 'getPrevious')) {
-                       $ex     = $ex->getPrevious();
-                       if ($ex !== null && $ex instanceof Exception) {
-                               $fullTrace .= sprintf('%s%s: %s', PHP_EOL, 'Caused by', $this->getExceptionAsString($ex));
-                       }
-               }               
-               return $fullTrace;
-       }
-       
-       protected function getExceptionAsString(Exception $ex) {
-               return sprintf('%s: %s%s%s' ,get_class($ex), $ex->getMessage(), PHP_EOL, $ex->getTraceAsString());                
+               $strRep  = 'Throwable('.get_class($o).'): '.$o->getMessage().' in '.$o->getFile().' on line '.$o->getLine();
+               $strRep .= PHP_EOL.$o->getTraceAsString();
+               
+               if (method_exists($o, 'getPrevious') && $o->getPrevious() !== null) {
+                       $strRep .= PHP_EOL.'Caused by: '.$this->render($o->getPrevious());                      
+               }
+               
+               return $strRep;         
        }
 }
 ?>
\ No newline at end of file
index 3a219cc..4df563d 100644 (file)
@@ -126,8 +126,7 @@ class LoggerLoggingEventTest extends PHPUnit_Framework_TestCase {
                
                self::assertTrue($ti instanceof LoggerThrowableInformation);                            
                
-               $expected = array('Message1');
                $result    = $ti->getStringRepresentation();
-               self::assertEquals($expected, $result);
+               self::assertType('array', $result);
        }
 }
index 502a134..79a8b6f 100644 (file)
 
 class LoggerThrowableInformationTest extends PHPUnit_Framework_TestCase {
        
-       public function testConstructor1() {
-               $rep  = array(
-                       'Message1',
-                       'Message2',
-                       'Message3'
-               );                
-               $tInfo    = new LoggerThrowableInformation($rep);
-               
-               $expected = $rep;
-               $result   = $tInfo->getStringRepresentation();
-               $this->assertEquals($expected, $result);
-       }
+       protected static $logger;
        
-       public function testConstructor2() {
-               $ex        = new LoggerThrowableInformationTestException('Message1');
-               $tInfo = new LoggerThrowableInformation($ex);
-               
-               $expected = array('Message1');
-               $result   = $tInfo->getStringRepresentation();
-               $this->assertEquals($expected, $result);
+       public static function setUpBeforeClass() {
+               self::$logger = Logger::getLogger('test');
        }
        
-       public function testConstructor3() {
-               $ex             = new LoggerThrowableInformationTestException('Message1');
-               $logger = Logger::getLogger('test');
-               $tInfo  = new LoggerThrowableInformation($ex, $logger);
-               
-               $expected = array('Message1');
-               $result   = $tInfo->getStringRepresentation();
-               $this->assertEquals($expected, $result);          
+       public static function tearDownAfterClass() {
+               self::$logger = null;
        }
        
-       public function testInvalidConstructor() {
-               try {
-                       $tInfo = new LoggerThrowableInformation('test');
-               } catch (InvalidArgumentException $ex) {
-                       return;
-               }
+       public function testConstructor() {
+               $ex = new Exception();
+               $tInfo = new LoggerThrowableInformation($ex);
                
-               $this->fail('Invalid constructor params should raise Exception');
+               $result   = $tInfo->getStringRepresentation();
+               $this->assertType('array', $result);
        }
        
        public function testExceptionChain() {
@@ -72,13 +48,15 @@ class LoggerThrowableInformationTest extends PHPUnit_Framework_TestCase {
                $ex3 = new LoggerThrowableInformationTestException('Message3', 0, $ex2);
 
                $tInfo    = new LoggerThrowableInformation($ex3);
-               $expected = array(
-                       'Message3',
-                       'Message2',
-                       'Message1'
-               );
                $result  = $tInfo->getStringRepresentation();
-               $this->assertEquals($expected, $result);
+               $this->assertType('array', $result);
+       }
+       
+       public function testGetThrowable() {
+               $ex = new LoggerThrowableInformationTestException('Message1');          
+               $tInfo = new LoggerThrowableInformation($ex);
+               $result = $tInfo->getThrowable();               
+               $this->assertEquals($ex, $result);
        }
 }
 
index 59d5d21..5ab9f26 100644 (file)
@@ -34,19 +34,19 @@ class LoggerRendererExceptionTest extends PHPUnit_Framework_TestCase {
         $rendered   = $exRenderer->render($ex3);
         
                $expected       = 3;        
-               $result         = substr_count($rendered, 'LoggerRendererExceptionTestException: Message');             
+               $result         = substr_count($rendered, 'Throwable(LoggerRendererExceptionTestException): Message');          
                $this->assertEquals($expected, $result);                
         
         $expected   = 2;        
-        $result     = substr_count($rendered, 'Caused by: LoggerRendererExceptionTestException:');        
+        $result     = substr_count($rendered, 'Caused by: Throwable(LoggerRendererExceptionTestException):');        
         $this->assertEquals($expected, $result);
         
         $expected   = 1;        
-        $result     = substr_count($rendered, 'Caused by: LoggerRendererExceptionTestException: Message2');        
+        $result     = substr_count($rendered, 'Caused by: Throwable(LoggerRendererExceptionTestException): Message2');        
         $this->assertEquals($expected, $result);
 
         $expected   = 1;        
-        $result     = substr_count($rendered, 'Caused by: LoggerRendererExceptionTestException: Message1');        
+        $result     = substr_count($rendered, 'Caused by: Throwable(LoggerRendererExceptionTestException): Message1');        
         $this->assertEquals($expected, $result);                
        }
 }