Author: grobmeier
Date: Fri Jun 18 04:55:34 2010
New Revision: 955838
URL: http://svn.apache.org/viewvc?rev=955838&view=rev
Log:
LOG4PHP-110 Added first part from Vladimir Gorej which improves the exception
handling (only exceptions are allowed, no arrays anymore) and simplifies
everything
Modified:
logging/log4php/trunk/src/main/php/Logger.php
logging/log4php/trunk/src/main/php/LoggerLoggingEvent.php
logging/log4php/trunk/src/main/php/LoggerThrowableInformation.php
logging/log4php/trunk/src/main/php/renderers/LoggerRendererException.php
logging/log4php/trunk/src/test/php/LoggerLoggingEventTest.php
logging/log4php/trunk/src/test/php/LoggerThrowableInformationTest.php
logging/log4php/trunk/src/test/php/renderers/LoggerRendererExceptionTest.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=955838&r1=955837&r2=955838&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/Logger.php (original)
+++ logging/log4php/trunk/src/main/php/Logger.php Fri Jun 18 04:55:34 2010
@@ -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));
}
Modified: logging/log4php/trunk/src/main/php/LoggerLoggingEvent.php
URL:
http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/LoggerLoggingEvent.php?rev=955838&r1=955837&r2=955838&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/LoggerLoggingEvent.php (original)
+++ logging/log4php/trunk/src/main/php/LoggerLoggingEvent.php Fri Jun 18
04:55:34 2010
@@ -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;
Modified: logging/log4php/trunk/src/main/php/LoggerThrowableInformation.php
URL:
http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/LoggerThrowableInformation.php?rev=955838&r1=955837&r2=955838&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/LoggerThrowableInformation.php (original)
+++ logging/log4php/trunk/src/main/php/LoggerThrowableInformation.php Fri Jun
18 04:55:34 2010
@@ -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;
Modified:
logging/log4php/trunk/src/main/php/renderers/LoggerRendererException.php
URL:
http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/renderers/LoggerRendererException.php?rev=955838&r1=955837&r2=955838&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/renderers/LoggerRendererException.php
(original)
+++ logging/log4php/trunk/src/main/php/renderers/LoggerRendererException.php
Fri Jun 18 04:55:34 2010
@@ -28,19 +28,14 @@
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
Modified: logging/log4php/trunk/src/test/php/LoggerLoggingEventTest.php
URL:
http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/LoggerLoggingEventTest.php?rev=955838&r1=955837&r2=955838&view=diff
==============================================================================
--- logging/log4php/trunk/src/test/php/LoggerLoggingEventTest.php (original)
+++ logging/log4php/trunk/src/test/php/LoggerLoggingEventTest.php Fri Jun 18
04:55:34 2010
@@ -126,8 +126,7 @@ class LoggerLoggingEventTest extends PHP
self::assertTrue($ti instanceof LoggerThrowableInformation);
- $expected = array('Message1');
$result = $ti->getStringRepresentation();
- self::assertEquals($expected, $result);
+ self::assertType('array', $result);
}
}
Modified: logging/log4php/trunk/src/test/php/LoggerThrowableInformationTest.php
URL:
http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/LoggerThrowableInformationTest.php?rev=955838&r1=955837&r2=955838&view=diff
==============================================================================
--- logging/log4php/trunk/src/test/php/LoggerThrowableInformationTest.php
(original)
+++ logging/log4php/trunk/src/test/php/LoggerThrowableInformationTest.php Fri
Jun 18 04:55:34 2010
@@ -24,46 +24,22 @@
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 ext
$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);
}
}
Modified:
logging/log4php/trunk/src/test/php/renderers/LoggerRendererExceptionTest.php
URL:
http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/renderers/LoggerRendererExceptionTest.php?rev=955838&r1=955837&r2=955838&view=diff
==============================================================================
---
logging/log4php/trunk/src/test/php/renderers/LoggerRendererExceptionTest.php
(original)
+++
logging/log4php/trunk/src/test/php/renderers/LoggerRendererExceptionTest.php
Fri Jun 18 04:55:34 2010
@@ -34,19 +34,19 @@ class LoggerRendererExceptionTest extend
$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);
}
}