Author: grobmeier
Date: Fri Apr 23 05:20:19 2010
New Revision: 937159
URL: http://svn.apache.org/viewvc?rev=937159&view=rev
Log:
LOG4PHP-109 contributed from Vladimir Gorej. Patch enables throwable
information associated with logging event.
Added:
logging/log4php/trunk/src/main/php/LoggerThrowableInformation.php
logging/log4php/trunk/src/main/php/renderers/LoggerRendererException.php
logging/log4php/trunk/src/test/php/LoggerThrowableInformationTest.php
logging/log4php/trunk/src/test/php/renderers/LoggerRendererExceptionTest.php
Modified:
logging/log4php/trunk/pom.xml
logging/log4php/trunk/src/changes/changes.xml
logging/log4php/trunk/src/main/php/Logger.php
logging/log4php/trunk/src/main/php/LoggerLoggingEvent.php
logging/log4php/trunk/src/test/php/LoggerLoggingEventTest.php
Modified: logging/log4php/trunk/pom.xml
URL:
http://svn.apache.org/viewvc/logging/log4php/trunk/pom.xml?rev=937159&r1=937158&r2=937159&view=diff
==============================================================================
--- logging/log4php/trunk/pom.xml (original)
+++ logging/log4php/trunk/pom.xml Fri Apr 23 05:20:19 2010
@@ -88,9 +88,11 @@
<email>chammers at apache.org</email>
</developer>
</developers>
-
<contributors>
- <contributor>
+ <contributor>
+ <name>Vladimir Gorej</name>
+ </contributor>
+ <contributor>
<name>Ivan Habunek</name>
</contributor>
<contributor>
Modified: logging/log4php/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/logging/log4php/trunk/src/changes/changes.xml?rev=937159&r1=937158&r2=937159&view=diff
==============================================================================
--- logging/log4php/trunk/src/changes/changes.xml (original)
+++ logging/log4php/trunk/src/changes/changes.xml Fri Apr 23 05:20:19 2010
@@ -24,6 +24,7 @@
</properties>
<body>
<release version="2.1" description="Stabilizing">
+ <action type="update" issue="LOG4PHP-109" by="Vladimir
Gorej">patch for Throwable information associated with logging event</action>
<action type="update" issue="LOG4PHP-111" by="Ivan
Habunek">Documentation: Problem using a custom ConversionPattern</action>
<action type="update" issue="LOG4PHP-108" by="Florian Platzer,
Christian Grobmeier">Add HTML line break to LoggerAppenderEcho output</action>
<action type="update" by="Ivan Habunek">Included new
LoggerLayoutPattern tests</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=937159&r1=937158&r2=937159&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/Logger.php (original)
+++ logging/log4php/trunk/src/main/php/Logger.php Fri Apr 23 05:20:19 2010
@@ -108,7 +108,9 @@ class Logger {
'LoggerRendererDefault' =>
'/renderers/LoggerRendererDefault.php',
'LoggerRendererObject' => '/renderers/LoggerRendererObject.php',
'LoggerRendererMap' => '/renderers/LoggerRendererMap.php',
+ 'LoggerRendererException' =>
'/renderers/LoggerRendererException.php',
'LoggerLocationInfo' => '/LoggerLocationInfo.php',
+ 'LoggerThrowableInformation' =>
'/LoggerThrowableInformation.php',
'LoggerLoggingEvent' => '/LoggerLoggingEvent.php',
'LoggerFilter' => '/LoggerFilter.php',
'LoggerFilterDenyAll' => '/filters/LoggerFilterDenyAll.php',
@@ -280,7 +282,12 @@ class Logger {
* @see LoggerLoggingEvent
*/
public function forcedLog($fqcn, $caller, $level, $message) {
- $this->callAppenders(new LoggerLoggingEvent($fqcn, $this,
$level, $message));
+ $throwable = null;
+ if ($caller !== null && $caller instanceof Exception) {
+ $throwable = $caller;
+ }
+
+ $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=937159&r1=937158&r2=937159&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/LoggerLoggingEvent.php (original)
+++ logging/log4php/trunk/src/main/php/LoggerLoggingEvent.php Fri Apr 23
05:20:19 2010
@@ -109,6 +109,11 @@ class LoggerLoggingEvent {
* @var LoggerLocationInfo Location information for the caller.
*/
private $locationInfo = null;
+
+ /**
+ * @var LoggerThrowableInformation log4php internal representation of
throwable
+ */
+ private $throwableInfo = null;
/**
* Instantiate a LoggingEvent from the supplied parameters.
@@ -121,8 +126,9 @@ class LoggerLoggingEvent {
* @param LoggerLevel $priority The level of this event.
* @param mixed $message The message of this event.
* @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) {
+ public function __construct($fqcn, $logger, $priority, $message,
$timeStamp = null, $throwable = null) {
$this->fqcn = $fqcn;
if($logger instanceof Logger) {
$this->logger = $logger;
@@ -142,6 +148,10 @@ class LoggerLoggingEvent {
$this->timeStamp = floatval(time());
}
}
+
+ if ($throwable !== null && $throwable instanceof Exception) {
+ $this->throwableInfo = new
LoggerThrowableInformation($throwable);
+ }
}
/**
@@ -331,10 +341,10 @@ class LoggerLoggingEvent {
}
/**
- * @return mixed null
+ * @return mixed LoggerThrowableInformation
*/
public function getThrowableInformation() {
- return null;
+ return $this->throwableInfo;
}
/**
Added: logging/log4php/trunk/src/main/php/LoggerThrowableInformation.php
URL:
http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/LoggerThrowableInformation.php?rev=937159&view=auto
==============================================================================
--- logging/log4php/trunk/src/main/php/LoggerThrowableInformation.php (added)
+++ logging/log4php/trunk/src/main/php/LoggerThrowableInformation.php Fri Apr
23 05:20:19 2010
@@ -0,0 +1,74 @@
+<?php
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @package log4php
+ */
+
+/**
+ * The internal representation of throwables.
+ *
+ * @package log4php
+ * @since 2.1
+ */
+class LoggerThrowableInformation {
+
+ /** @var Exception Throwable to log */
+ private $throwable;
+
+ /** @var array Array of throwable messages */
+ private $throwableArray;
+
+ /** @var Logger reference */
+ private $logger;
+
+ /**
+ * Create a new instance
+ *
+ * @param $throwable - a throwable either as array or as a exception
+ */
+ public function __construct($throwable) {
+ if(is_array($throwable)) {
+ $this->throwableArray = $throwable;
+ } else if($throwable instanceof Exception) {
+ $this->throwable = $throwable;
+ } else {
+ throw new InvalidArgumentException();
+ }
+ }
+
+ /**
+ * @desc Returns string representation of throwable
+ *
+ * @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();
+ }
+ }
+ }
+
+ return $this->throwableArray;
+ }
+}
+?>
\ No newline at end of file
Added: 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=937159&view=auto
==============================================================================
--- logging/log4php/trunk/src/main/php/renderers/LoggerRendererException.php
(added)
+++ logging/log4php/trunk/src/main/php/renderers/LoggerRendererException.php
Fri Apr 23 05:20:19 2010
@@ -0,0 +1,46 @@
+<?php
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @package log4php
+ */
+
+/**
+ * Exception renderer
+ *
+ * @package log4php
+ * @subpackage renderers
+ * @since 2.1
+ */
+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());
+ }
+}
+?>
\ 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=937159&r1=937158&r2=937159&view=diff
==============================================================================
--- logging/log4php/trunk/src/test/php/LoggerLoggingEventTest.php (original)
+++ logging/log4php/trunk/src/test/php/LoggerLoggingEventTest.php Fri Apr 23
05:20:19 2010
@@ -40,13 +40,15 @@ class LoggerLoggingEventTestCaseLayout e
}
public function format(LoggerLoggingEvent $event) {
- LoggerLoggingEventTest::$locationInfo =
$event->getLocationInformation();
+ LoggerLoggingEventTest::$locationInfo =
$event->getLocationInformation();
+ LoggerLoggingEventTest::$throwableInfo =
$event->getThrowableInformation();
}
}
class LoggerLoggingEventTest extends PHPUnit_Framework_TestCase {
public static $locationInfo;
+ public static $throwableInfo;
public function testConstructWithLoggerName() {
$l = LoggerLevel :: getLevelDebug();
@@ -89,5 +91,43 @@ class LoggerLoggingEventTest extends PHP
self::assertEquals($li->getMethodName(), __FUNCTION__);
}
-
+
+ public function testGetThrowableInformation1() {
+ $hierarchy = Logger::getHierarchy();
+ $root = $hierarchy->getRootLogger();
+
+ $a = new LoggerLoggingEventTestCaseAppender('A1');
+ $a->setLayout( new LoggerLoggingEventTestCaseLayout() );
+ $root->addAppender($a);
+
+ $logger = $hierarchy->getLogger('test');
+ $logger->debug('test');
+ $hierarchy->shutdown();
+
+ $ti = self::$throwableInfo;
+
+ self::assertEquals($ti, null);
+ }
+
+ public function testGetThrowableInformation2() {
+ $hierarchy = Logger::getHierarchy();
+ $root = $hierarchy->getRootLogger();
+
+ $a = new LoggerLoggingEventTestCaseAppender('A1');
+ $a->setLayout( new LoggerLoggingEventTestCaseLayout() );
+ $root->addAppender($a);
+
+ $ex = new Exception('Message1');
+ $logger = $hierarchy->getLogger('test');
+ $logger->debug('test', $ex);
+ $hierarchy->shutdown();
+
+ $ti = self::$throwableInfo;
+
+ self::assertTrue($ti instanceof LoggerThrowableInformation);
+
+ $expected = array('Message1');
+ $result = $ti->getStringRepresentation();
+ self::assertEquals($expected, $result);
+ }
}
Added: logging/log4php/trunk/src/test/php/LoggerThrowableInformationTest.php
URL:
http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/LoggerThrowableInformationTest.php?rev=937159&view=auto
==============================================================================
--- logging/log4php/trunk/src/test/php/LoggerThrowableInformationTest.php
(added)
+++ logging/log4php/trunk/src/test/php/LoggerThrowableInformationTest.php Fri
Apr 23 05:20:19 2010
@@ -0,0 +1,103 @@
+<?php
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @category tests
+ * @package log4php
+ * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License,
Version 2.0
+ * @version SVN: $Id$
+ * @link http://logging.apache.org/log4php
+ */
+
+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);
+ }
+
+ public function testConstructor2() {
+ $ex = new
LoggerThrowableInformationTestException('Message1');
+ $tInfo = new LoggerThrowableInformation($ex);
+
+ $expected = array('Message1');
+ $result = $tInfo->getStringRepresentation();
+ $this->assertEquals($expected, $result);
+ }
+
+ 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 function testInvalidConstructor() {
+ try {
+ $tInfo = new LoggerThrowableInformation('test');
+ } catch (InvalidArgumentException $ex) {
+ return;
+ }
+
+ $this->fail('Invalid constructor params should raise
Exception');
+ }
+
+ public function testExceptionChain() {
+ $ex1 = new LoggerThrowableInformationTestException('Message1');
+ $ex2 = new LoggerThrowableInformationTestException('Message2',
0, $ex1);
+ $ex3 = new LoggerThrowableInformationTestException('Message3',
0, $ex2);
+
+ $tInfo = new LoggerThrowableInformation($ex3);
+ $expected = array(
+ 'Message3',
+ 'Message2',
+ 'Message1'
+ );
+ $result = $tInfo->getStringRepresentation();
+ $this->assertEquals($expected, $result);
+ }
+}
+
+
+if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
+ class LoggerThrowableInformationTestException extends Exception { }
+} else {
+ class LoggerThrowableInformationTestException extends Exception {
+
+ protected $previous;
+
+ public function __construct($message = '', $code = 0, Exception
$previous = null) {
+ parent::__construct($message, $code);
+ $this->previous = $previous;
+ }
+
+ public function getPrevious() {
+ return $this->previous;
+ }
+ }
+}
+?>
\ No newline at end of file
Added:
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=937159&view=auto
==============================================================================
---
logging/log4php/trunk/src/test/php/renderers/LoggerRendererExceptionTest.php
(added)
+++
logging/log4php/trunk/src/test/php/renderers/LoggerRendererExceptionTest.php
Fri Apr 23 05:20:19 2010
@@ -0,0 +1,71 @@
+<?php
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @category tests
+ * @package log4php
+ * @subpackage renderers
+ * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License,
Version 2.0
+ * @version SVN: $Id$
+ * @link http://logging.apache.org/log4php
+ */
+
+class LoggerRendererExceptionTest extends PHPUnit_Framework_TestCase {
+
+ public function testRender() {
+ $exRenderer = new LoggerRendererException();
+ $ex1 = new
LoggerRendererExceptionTestException('Message1');
+ $ex2 = new
LoggerRendererExceptionTestException('Message2', 0, $ex1);
+ $ex3 = new
LoggerRendererExceptionTestException('Message3', 0, $ex2);
+
+ $rendered = $exRenderer->render($ex3);
+
+ $expected = 3;
+ $result = substr_count($rendered,
'LoggerRendererExceptionTestException: Message');
+ $this->assertEquals($expected, $result);
+
+ $expected = 2;
+ $result = substr_count($rendered, 'Caused by:
LoggerRendererExceptionTestException:');
+ $this->assertEquals($expected, $result);
+
+ $expected = 1;
+ $result = substr_count($rendered, 'Caused by:
LoggerRendererExceptionTestException: Message2');
+ $this->assertEquals($expected, $result);
+
+ $expected = 1;
+ $result = substr_count($rendered, 'Caused by:
LoggerRendererExceptionTestException: Message1');
+ $this->assertEquals($expected, $result);
+ }
+}
+
+if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
+ class LoggerRendererExceptionTestException extends Exception { }
+} else {
+ class LoggerRendererExceptionTestException extends Exception {
+
+ protected $previous;
+
+ public function __construct($message = '', $code = 0, Exception
$previous = null) {
+ parent::__construct($message, $code);
+ $this->previous = $previous;
+ }
+
+ public function getPrevious() {
+ return $this->previous;
+ }
+ }
+}
+?>
\ No newline at end of file